Lm th? no ? k?t n?i v?i c s? d? li?u v ch?y m?t l?nh b?ng cch s? d?ng ADO.NET 2005 v Visual C# 2005 ho?c b?ng cch s? d?ng ADO.NET v Visual C#.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 306636 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t t?ng b?c ny cho b?n th?y lm th? no ? s? d?ng ADO.NET 2005 ho?c ADO.NET ? k?t n?i v?i c s? d? li?u v ch?y l?nh.

Yu c?u

Cc danh sch sau y v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, m?ng c s? h? t?ng, v gi d?ch v? m b?n c?n:
  • Microsoft Windows Server 2003, Microsoft Windows 2000 Chuyn nghi?p, Windows 2000 Server, Windows 2000 Advanced Server, ho?c Microsoft My ch? Windows NT 4.0
  • Microsoft Visual Studio 2005 ho?c Microsoft Visual Studio .NET
i?u ny bi gi? ?nh r?ng b?n ? quen thu?c v?i cc ch? ? sau:
  • C s? d? li?u thu?t ng?
  • C?u trc Query Language (SQL)

Lm th? no ? ch?y m?t l?nh

L?nh ?c ban hnh ?i v?i c s? d? li?u ? c hnh ?ng ch?ng l?i cc c?a hng d? li?u v bao g?m b?t k? tuyn b? r?ng c th? ?c ban hnh ch?ng l?i m?t c s? d? li?u. B?n c th? s? d?ng cc OleDbCommand ho?c cc SqlCommand cc l?p h?c ? c ?c m?t cu l?nh ? lu tr? d? li?u c?a b?n, v OleDbCommand c th? ?c c? th? ? lu tr? d? li?u. Bi vi?t ny ch?ng t? c? hai cc SqlClient l?p (k?t n?i v?i m?t my tnh ang ch?y Microsoft SQL My ch?) v cc OleDb l?p h?c (cho b?t k? c s? d? li?u c m?t tr?nh i?u khi?n OLE DB ho?c ODBC s?n) trong ADO.NET. Tuy nhin, cc m? th?ng l nh nhau cho c? hai.

V?i ADO, b?n c th? pht hnh l?nh thng qua cc Ch? huy, cc K?t n?i, ho?c cc RecordSet ?i t?ng. Trong ADO.NET, ch? cc Ch? huy cc ?i t?ng)SqlCommand ho?c OleDbCommand) ch?y l?nh.

? ch?y l?nh, h?y lm theo cc b?c sau:
  1. Lm theo cc b?c sau ? t?o ra m?t ?ng d?ng giao di?n i?u khi?n m?i trong Microsoft Visual C# 2005 ho?c trong Microsoft Visual C#.NET:
    1. B?t ?u Microsoft Visual Studio 2005 ho?c Microsoft Visual Ph?ng thu.NET.
    2. Trn cc Tp tr?nh n, i?m ?n M?i, sau b?m D an.
    3. Trong cc D? n m?i h?p tho?i h?p, b?m vo Visual C# cc d? n d?i Cc lo?i d? n, sau b?m Giao di?n i?u khi?n ?ng d?ng d?i Khun mu.

      Chu y Trong Visual Studio 2005, nh?p vo Visual C# d?i Cc lo?i d? n trong cc D? n m?i h?p tho?i, v sau nh?p vo Giao di?n i?u khi?n ?ng d?ng d?i Khun mu.
  2. ?m b?o r?ng d? n c?a b?n c ch?a m?t tham chi?u ?n cc System.Data khng gian tn, v thm m?t ti li?u tham kh?o n?u n khng.
  3. S? d?ng cc b?ng cch s? d?ng tuyn b? v? s? Cac vn v System.Data khng gian tn do b?n khng c?n ph?i v?t qua v?ng lo?i t? khai trong nh?ng khng gian tn sau trong m? c?a b?n. B?n c?ng c th? bao g?m System.Data.SqlClient ho?c System.Data.OleDb, ty thu?c vo ci no b?n ang s? d?ng.
    using System;
    using System.Data;
    using System.Data.SqlClient;
    					
  4. Tr?c khi b?n c th? t?o m?t k?t n?i ?n c s? d? li?u, b?n ph?i c m?t k?t n?i chu?i. K?t n?i chu?i ch?a t?t c? cc thng tin b?n c?n thi?t l?p k?t n?i c s? d? li?u, bao g?m c? tn my ch?, tn c s? d? li?u, tn ng?i dng v m?t kh?u. V d?, sau y k?t n?i chu?i i?m v?i m?t my tnh ?a phng ang ch?y SQL My ch?:

    ?i v?i cc k?t n?i OleDb:

    Chu y Ng?i s? d?ng ID <uid>ph?i c quy?n thch h?p ? th?c hi?n cc ho?t ?ng trn c s? d? li?u.</uid>
    Provider=SQLOLEDB.1;User ID=<UID>;Initial Catalog=pubs;Data Source=(local)
    						
    ?i v?i cc k?t n?i SqlClient:
    User ID=<UID>;Initial Catalog=pubs;Data Source=(local)
    						
    Chu y N?u b?n c?n thm tr? gip xc ?nh cc k?t n?i chu?i cho c s? d? li?u c?a b?n, t?m ki?m cho "ConnectionString" trn Microsoft Developer Network (MSDN) Th vi?n:
    http://MSDN.Microsoft.com/en-US/Default.aspx
  5. Visual Studio t?o ra m?t l?p h?c t?nh v m?t s?n ph?m no Main() th? t?c. Khai bo m?t bi?n chu?i, v lu tr? nhan k?t n?i chu?i cho c s? d? li?u trong th? t?c ny.

    Chu y Ng?i s? d?ng ID <uid>ph?i c quy?n thch h?p ? th?c hi?n cc ho?t ?ng trn c s? d? li?u.</uid>
    class Class1
    {
    	static void Main(string[] args)
    	{
    		string sConnectionString = 
    "User ID=<UID>;Initial Catalog=pubs;Data Source=(local)";
    	}
    }
    					
  6. B?ng cch s? d?ng ny chu?i k?t n?i, t?o ra m?t m?i OleDbConnection ho?c SqlConnection ?i t?ng, v g?i c?a n M? phng php thi?t l?p k?t n?i c s? d? li?u c?a b?n:
    SqlConnection objConn = new SqlConnection(sConnectionString);
    objConn.Open();
    					
  7. T?o ra m?t SqlCommand ho?c OleDbCommand ?i t?ng, v v?t qua trong l?nh m b?n mu?n ch?y v cc ?i t?ng k?t n?i b?n ? t?o ? b?c tr?c. Cc m?u sau y M? i trong tuyn b? CHN:
    string sSQL = "INSERT INTO Employee " + 
      "(emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date) " + 
      "VALUES ('MSD12923F', 'Duncan', 'W', 'Mackenzie', 10, 82,'0877','2001-01-01')";
    SqlCommand objCmd = new SqlCommand(sSQL,objConn);
    					
  8. Sau khi b?n t?o cc SqlCommand ho?c OleDbCommand ?i t?ng, b?n c th? g?i cc ExecuteNonQuery phng php ? ch?y l?nh m n ?i di?n. ExecuteNonQuery ?c thi?t k? cho cc l?nh khng tr? v? b?t k? k?t qu? (ch?ng h?n nh vi?c xo, C?p Nh?t v thm cu). N?u cc bo co ch?y m khng c?n nm ngo?i l? (xem o?n m? sau), cu l?nh ? ?c th?c hi?n thnh cng ch?ng l?i c s? d? li?u.
    objCmd.ExecuteNonQuery();
    					
  9. Lu d? n c?a b?n. Trn cc G? l?i tr?nh n, nh?p vo B?t ?u ch?y l?nh c?a b?n ?i v?i c s? d? li?u.

Lm th? no ? s? d?ng tham s?

Khi b?n ch?y l?nh ?i v?i c s? d? li?u (v d? nh C?p Nh?t, cc CHN, v xo b? bo co ho?c cc cu?c g?i ?n th? t?c ?c lu tr?), nh?ng l?nh th?ng xuyn ?c vec. i?u ny cho php cc l?nh ?c t?o ra m?t trong nh?ng th?i gian nhng th?c hi?n nhi?u l?n v?i cc gi tr? khc nhau ?c chn vo thay v? cc tham s?. Xem xt tuyn b? DELETE tng ?ng v?i cc CHN n?i dung ?c s? d?ng trong ph?n tr?c:
string sSQL = "DELETE FROM Employee WHERE emp_id = @emp_id"
				
Tn tham s? ("@ emp_id") trong tuyn b? ny DELETE ?i di?n cho m?t tham s? hn b?n c th? thay th? v?i cc gi tr? khc nhau m?i khi b?n ch?y cc b? ch? huy.

S? d?ng tham s? v?i l?nh c?a b?n, h?y lm theo cc b?c sau:
  1. T?o c?a b?n OleDbConnection ho?c SqlConnection ?i t?ng, nh b?n ? lm trong nh?ng "Cach ch?y l?nh"ph?n.
  2. Thay th? cc gi tr? v?i bi (v d?, "@ emp_id" ho?c "@ fname") ? cc vn b?n ch? huy c?a b?n s? d?ng tham s?. Xem tuyn b? DELETE tr?c khi cc b?c ny cho m?t v d?.
  3. T?o c?a b?n OleDbCommand ho?c SqlCommand ?i t?ng, v v?t qua trong cc ?i t?ng k?t n?i m b?n t?o ra trong cc b?c ?u tin v ch? huy vn b?n c ch?a cc tham s? bi.
  4. Cho m?i tham s?, thm m?t ?i t?ng tham s? cho l?nh b? su t?p cc tham s? c?a ?i t?ng. Cho m?i tham s?, b?n ph?i ch? ?nh m?t tn v ki?u d? li?u.
    objCmd.Parameters.Add("@emp_id", SqlDbType.Char, 9);
    					
  5. Th? t?c ?c lu tr? c th? c cc tham s? tr? v? gi tr? v ?u ra cc thng s?. B?n c?ng ph?i thi?t l?p m?t gi tr? cho m?i tham s? ?u vo tr?c khi b?n c th? ch?y truy v?n:
    objCmd.Parameters["@emp_id"].Value = "MSD12923F";
    					
  6. Ch?y truy v?n nh sau:
    try 
    {
    	objCmd.ExecuteNonQuery();
    }
    catch (System.Exception e) 
    {
    	Console.WriteLine(e.Message);
    }
    Console.WriteLine("Record Deleted");
    					

Danh sch m? hon ch?nh

Chu y B?n ph?i thay ?i ID ng?i s? d?ng =<uid> v?i cc gi tr? ng tr?c khi b?n ch?y m? ny. H?y ch?c ch?n r?ng <uid>c quy?n truy c?p thch h?p ? th?c hi?n thao tc ny trn c s? d? li?u.</uid> </uid>
using System;
using System.Data;
using System.Data.SqlClient;

    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
	static void Main(string[] args)
	{
		AddRecord();
		RemoveRecord();
		Pause();
	}

	static void Pause()
	{
		Console.WriteLine("Press Enter To Continue....");
		Console.ReadLine();
	}

	static void AddRecord()
	{
		string sConnectionString = "User ID=<UID>;Initial Catalog=pubs;Data Source=(local)";
		SqlConnection objConn = new SqlConnection(sConnectionString);
		objConn.Open();
		string sSQL = "INSERT INTO Employee " + 
		  "(emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date) " + 
	          "VALUES ('MSD12923F', 'Duncan', 'W', 'Mackenzie', 10, 82,'0877','2001-01-01')";
		SqlCommand objCmd = new SqlCommand(sSQL,objConn);
		try 
		{
			objCmd.ExecuteNonQuery();
			}
		catch (System.Exception e) 
		{
			Console.WriteLine(e.Message);
		}
		Console.WriteLine("Record Added");
	}

	static void RemoveRecord()
	{
		string sConnectionString = "User ID=<UID>;Initial Catalog=pubs;Data Source=(local)";
		SqlConnection objConn = new SqlConnection(sConnectionString);
		objConn.Open();
		string sSQL = "DELETE FROM Employee WHERE emp_id = @emp_id";
		SqlCommand objCmd = new SqlCommand(sSQL,objConn);
		objCmd.Parameters.Add("@emp_id", SqlDbType.Char, 9);
		objCmd.Parameters["@emp_id"].Value = "MSD12923F";
		try 
		{
			objCmd.ExecuteNonQuery();
		}
		catch (System.Exception e) 
		{
			Console.WriteLine(e.Message);
		}
		Console.WriteLine("Record Deleted");
	}
} 
				

THAM KH?O

? bi?t thm chi ti?t v? lm th? no ? s? d?ng ADO.NET, c s? d? li?u l?nh v cc th? t?c ?c lu tr?, truy c?p vo trang Web Microsoft sau y:
SQL Server 2000 ?c lu tr? th? t?c
http://msdn2.Microsoft.com/en-US/Library/aa214299 (SQL.80) .aspx

"L?n vo truy c?p d? li?u" MSDN Voices c?t
http://msdn2.Microsoft.com/en-US/Library/ms810295.aspx

ADO.NET cho cc l?p tr?nh vin ADO
http://msdn2.Microsoft.com/en-US/Library/ms973217.aspx

MSDN tr?c tuy?n.Trung tm nh pht tri?n NET
http://MSDN.Microsoft.com/net
? bi?t thm chi ti?t, xem cu?n sch sau y:
Sharp, John v Jon Jagger. Microsoft Visual C#.NET t?ng b?c. Microsoft Press, 2003.
? bi?t thm chi ti?t, xem o t?o Microsoft sau & Ch?ng nh?n kha h?c:
2389 L?p tr?nh v?i ADO.NET
http://www.Microsoft.com/Learning/syllabi/en-US/2389Bfinal.mspx

Thu?c tnh

ID c?a bi: 306636 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005
T? kha:
kbhowtomaster kbsqlclient kbsystemdata kbmt KB306636 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:306636

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com