Chèn dữ liệu vào CSDL sử dụng Parameters

Published by Việt Coding on

 

using System;
using System.Data;
using System.Data.SqlClient;

class UsingParameters
{
	public static void Main()
	{
		SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

		mySqlConnection.Open();

		SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
		mySqlCommand.CommandText =
									"INSERT INTO Employee (" +
									" ID, FirstName, LastName" +
									") VALUES (" +
									" @ID, @FirstName, @LastName" +
									")";

		mySqlCommand.Parameters.Add("@ID", SqlDbType.NChar, 2);
		mySqlCommand.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10);
		mySqlCommand.Parameters.Add("@LastName", SqlDbType.NVarChar, 10);

		mySqlCommand.Parameters["@ID"].Value = "9";
		mySqlCommand.Parameters["@FirstName"].Value = "New";
		mySqlCommand.Parameters["@LastName"].IsNullable = true;
		mySqlCommand.Parameters["@LastName"].Value = "New";

		mySqlCommand.ExecuteNonQuery();
		Console.WriteLine("Successfully added row to Customers table");

		mySqlConnection.Close();
	}
}

Quảng cáo tài trợ


Việt Coding

Là một người đam mê lập trình, tôi vọc vạch đủ thứ liên quan đến lập trình cho thoả chí tò mò. Hiện làm chủ yếu ở mảng phát triển ứng dụng di động cho iOS và Android với React Native. Thỉnh thoảng vọc vạch mấy thứ liên quan đến Internet of Things như Smart Home. Đang nghịch mấy con Raspberry Pi và thấy nó cũng thú vị :)

2 Comments

xem tu vi · 04/01/2012 at 22:06

thấy ten web C# chạy trên nền IIS la ko co hỗ trợ .htacess . có cách nào để nó chạy duoc ko bạn

    Việt Coding · 05/01/2012 at 14:51

    Với IIS7 bạn có thể sử dụng rewrite url. Bạn nên liên hệ với bên cung cấp hosting để biết thêm chi tiết.

Leave a Reply to Việt Coding Cancel reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax