Lm th? no ? c?p nh?t b? my c s? d? li?u t? m?t ?i t?ng b? d? li?u b?ng cch s? d?ng Visual C# 2005 ho?c Visual C# .NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 307587 - 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 ? c m?t b? d? li?u c ch?a d? li?u (m ?c t?i t? m?t c s? d? li?u), lm th? no ? s?a ?i d? li?u , v sau lm th? no ? g?i n tr? l?i b? my c s? d? li?u ? c?p nh?t ngu?n g?c.

?i t?ng b? d? li?u , m?t ph?n quan tr?ng c?a truy c?p d? li?u trong Microsoft Khun kh? .NET, l cc ?i t?ng trong b? nh? c th? t? ch?c bn, xem, v m?i quan h?.

Yu c?u

Danh sch sau phc th?o ?c ? ngh? ph?n c?ng, ph?n m?m, c s? h? t?ng m?ng v gi b?n ghi d?ch v? m b?n c?n:
  • Microsoft Windows Server 2003, Microsoft Windows 2000Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 AdvancedServer, ho?c Microsoft Windows NT 4.0 my ch?
  • Microsoft SQL Server Phin b?n 7.0, Microsoft SQL Server2000, ho?c Microsoft d? li?u ?ng c (MSDE) v?i cc qun r?u m?u databaseinstalled
  • Microsoft Visual Studio 2005 ho?c Microsoft Visual Studio.NET
Bi vi?t ny gi? ?nh r?ng b?n ang quen thu?c v?i cc ch? ? sau:
  • b? my c s? d? li?u thu?t ng?
  • Ngn ng? truy v?n c c?u trc (SQL)

Lm th? no ? c?p nh?t b? my c s? d? li?u t? m?t ?i t?ng b? d? li?u

Ph?n ny ch?ng t? lm th? no ? s? d?ng ?i t?ng b? d? li?u ? c?p nh?t d? li?u trong b? my c s? d? li?u. N l quan tr?ng c?n nh? r?ng b?n c?ng c th? s? d?ng m?t ?i t?ng SqlCommand chn, C?p Nh?t v xa d? li?u trong b? my c s? d? li?u tr?c ti?p.

? gip b?n hi?u bi vi?t ny, b?m s? bi vi?t d?i y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
314145 Lm th? no ? c m?t b? d? li?u ?i t?ng t? m?t b? my c s? d? li?u b?ng cch s? d?ng Visual C# .NET
M?t s? trong nh?ng ch? ? ?c b?o hi?m trong 314145 bao g?m lm th? no ? l?y d? li?u t? b? my c s? d? li?u v vo m?t b? d? li?u, v lm th? no b? d? li?u l ring bi?t v khc bi?t v?i b? my c s? d? li?u.

Sau khi s? li?u ?c t?i, b?n c th? s?a ?i cc d? li?u. S? li?u s? theo d?i nh?ng thay ?i ny. ?i t?ng b? d? li?u c th? ?c coi l m?t trong b? nh? cache c?a d? li?u ?c l?y t? m?t b? my c s? d? li?u. ?i t?ng b? d? li?u bao g?m m?t t?p h?p cc b?ng, m?i quan h?, v kh khn.

? c?p nh?t m?t t?p d? li?u v g?i nh?ng thng tin C?p Nh?t v? cho b? my c s? d? li?u, h?y lm theo cc b?c sau:
  1. B?t ?u Visual Studio 2005 ho?c Visual Studio net.
  2. T?o ra m?t ?ng d?ng giao di?n i?u khi?n m?i trong Visual C#. VisualStudio t?o ra m?t l?p h?c t?nh theo m?c ?nh v m?t th? t?c Main() s?n ph?m no.
  3. ?m b?o r?ng cc d? n c ch?a m?t tham chi?u ?n cc khng gian tn h? th?ng v System.Data . S? d?ng cc bo co b?ng cch s? d?ng trn khng gian tn h? th?ng, System.Data, v System.Data.SqlClient do b?n khng c?n ph?i v?t qua v?ng lo?i declarationsfrom cc khng gian tn sau trong m? c?a b?n. B?n ph?i s? d?ng priorto bo co b?t k? khai bo khc.
    using System;
    using System.Data;
    using System.Data.SqlClient;
    					
  4. Tr?c khi b?n c th? s?a ?i cc d? li?u v g?i backto thay ?i b? my c s? d? li?u, b?n ph?i t?i cc thng tin vo b? d? li?u. ? tr?nh chi ti?t, h?y xem314145. ? trnh trng l?p, m? trong ny stepis khng tr?nh by chi ti?t.

    Chu?i k?t n?i i?m followingcode ? m?t my ch? SQL ?c ?t trn my tnh ?a phng (ho?c thecomputer v? tr m? ang ch?y) ? tm t?t, m?t k?t n?i ?c t?o ra, andthen m?t b? chuy?n ?i d? li?u ?c t?o ra, ?c s? d?ng ? i?n vo b? d? li?u v?i d? li?u.
    Lu ? B?n ph?i thay ?i ID ng?i dng <username>v m?t kh?u<strongpassword> cc gi tr? chnh xc tr?c khi b?n ch?y m? ny. ?m b?o r?ng thatUser ID c s? cho php thch h?p ? th?c hi?n thao tc ny trn thedatabase.</strongpassword> </username>
    string sConnectionString;
    
    // Modify the following string to correctly connect to your SQL Server.
    sConnectionString = "Password=<strong password>;User ID=<username>;"
    	+ "Initial Catalog=pubs;"
    	+ "Data Source=(local)";
    
    SqlConnection objConn
    	= new SqlConnection(sConnectionString);
    objConn.Open();
    
    // Create an instance of a DataAdapter.
    SqlDataAdapter daAuthors 
    	= new SqlDataAdapter("Select * From Authors", objConn);
    
    // Create an instance of a DataSet, and retrieve data from the Authors table.
    DataSet dsPubs = new DataSet("Pubs");
    daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
    daAuthors.Fill(dsPubs,"Authors");
    					
  5. By gi? m cc d? li?u ?c t?i, b?n c th? s?a ?i n. C aremany cch ? thm m?t hng (ho?c h? s). M?u m? ny s? d?ng m?t stepprocedure ba:
    • C ?c m?t ?i t?ng m?i c?a DataRow t? DataTable.
    • Thi?t l?p gi tr? tr?ng DataRow khi c?n thi?t.
    • V?t qua r?ng ?i t?ng m?i vo phng php Thm b? su t?p DataTable.Rows .
    Dn o?n m? sau sau khi m? trong b?c 4:
    //****************
    // BEGIN ADD CODE 
    // Create a new instance of a DataTable.
    DataTable tblAuthors;
    tblAuthors = dsPubs.Tables["Authors"];
    
    DataRow drCurrent;
    // Obtain a new DataRow object from the DataTable.
    drCurrent = tblAuthors.NewRow();
    
    // Set the DataRow field values as necessary.
    drCurrent["au_id"] = "993-21-3427";
    drCurrent["au_fname"] = "George";
    drCurrent["au_lname"] = "Johnson";
    drCurrent["phone"] = "800 226-0752";
    drCurrent["address"] = "1956 Arlington Pl.";
    drCurrent["city"] = "Winnipeg";
    drCurrent["state"] = "MB";
    drCurrent["contract"] = 1;
    
    // Pass that new object into the Add method of the DataTable.
    tblAuthors.Rows.Add(drCurrent);
    Console.WriteLine("Add was successful, Click any key to continue!!");
    Console.ReadLine();
    
    // END ADD CODE  
    					
  6. ? ch?nh s?a hng hi?n c, c ?c cc ?i t?ng DataRow thch h?p, v sau cung c?p gi tr? m?i cho m?t ho?c nhi?u c?t. Youmust ?u tin t?m th?y d?ng ng, l d? dng hn nhi?u b?i v? b?n t?i theschema c?a b?ng d? li?u (cu?c g?i ?n FillSchema trong b?c 4). V?i l?c ? t?i ch?, b?ng bi?t whichcolumn l kho chnh c?a n, v phng php t?m th?y b? su t?p hng c s?n.

    Phng php T?m tr? v? cc ?i t?ng DataRow v?i m?t gi tr? c? th? trong kho chnh c?a n (trong tr?ng h?p ny, au_id). Sau khi b?n c m DataRow, b?n c th? s?a ?i cc c?t. B?n khng c?n ph?i b?c themodifications trong BeginEdit v EndEdit, nhng i?u ny n gi?n ho vi?c b? d? li?u ? lm v cho php b? d? li?u ? th?c hi?n ki?m tra xc nh?n c?a n cng m?t lc khi EndEdit ?c g?i l. Dn o?n m? sau sau khi m? ADD:
    //*****************
    // BEGIN EDIT CODE 
    
    drCurrent = tblAuthors.Rows.Find("213-46-8915");
    drCurrent.BeginEdit();
    drCurrent["phone"] = "342" + drCurrent["phone"].ToString().Substring(3);
    drCurrent.EndEdit();
    Console.WriteLine("Record edited successfully, Click any key to continue!!");
    Console.ReadLine();
    
    // END EDIT CODE  
    					
  7. ? c?p nh?t b? my c s? d? li?u ban ?u v?i t?t c? nh?ng thay ?i ny, v?t qua b? d? li?u vo C?p Nh?t phng php ?i t?ng DataAdapter .

    Tuy nhin, tr?c khi b?n c th? g?i C?p Nh?t, b?n ph?i thi?t l?p cc thu?c tnh InsertCommand, UpdateCommand, v DeleteCommand c?a ?i t?ng DataAdapter . B?n c th? t? vi?t SQL v c tr trong cc threeproperties v?i tng ?ng SqlCommand cc ?i t?ng, nhng b?n c?ng c th? s? d?ng Visual Studio .NET generatethese ba l?nh t? ?ng.

    ? t?o ra commandswhen c?n thi?t c?n thi?t, b?n ph?i t?o m?t th? hi?n c?a ?i t?ng SqlCommandBuilder v s? d?ng DataAdapter trong cc nh xy d?ng. N?u b?n mu?n s? d?ng phng php ny, m isillustrated trong m?u m? ? lm theo, b?n ph?i c chnh chnh informationavailable cho b?ng c?a b?n. ? truy c?p thng tin quan tr?ng chnh, g?i FillSchema, v sau thi?t l?p ti s?n MissingSchemaAction c?a b?n DataAdapter ? AddWithKey, ho?c ?t th? cng cc kho chnh trong m? c?a b?n. Dn thefollowing m? sau khi ch?nh s?a m?:
    //*****************
    // BEGIN SEND CHANGES TO SQL SERVER 
    
    SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);
    daAuthors.Update(dsPubs, "Authors");
    Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
    Console.ReadLine();
    
    // END SEND CHANGES TO SQL SERVER
    					
  8. ? xa m?t hng hon ton, s? d?ng phng php xa ?i t?ng DataRow . Lu ? r?ng b? su t?p hng c hai phng php, lo?i b? v RemoveAt, m d?ng nh ? xa d?ng nhng thay vo ch? c?n lo?i b? rowfrom b? su t?p. Phng ch? xa Php g?i xa c?a b?n quay l?i b? my c s? d liu ngun. Dn thefollowing m? sau khi m? g?i thay ?i ? SQL SERVER:
    //*****************
    //BEGIN DELETE CODE 
    
    drCurrent = tblAuthors.Rows.Find("993-21-3427");
    drCurrent.Delete();
    Console.WriteLine("Record deleted successfully, Click any key to continue!!"); 
    Console.ReadLine();
    
    //END DELETE CODE 
    					
  9. G?i cc thay ?i cho SQL Server ? lo?i b? anh ghi ?c g?i tr?c . Dn o?n m? sau sau khi xa m?:
    //*****************
    // CLEAN UP SQL SERVER
    daAuthors.Update(dsPubs, "Authors");
    Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
    Console.ReadLine();
    					
  10. Lu d? n c?a b?n.
  11. Trn cc g? l?i tr?nh n, nh?p vo B?t ?u ? ch?y cc d? n. Thng bo m?t s? tin th thoai h?p xu?t hi?n, m cho th?y s? ti?n b? c?a m? v cho php b?n xem l?i currentstate d? li?u ti?n c?a m?.

Danh sch m? hon ch?nh

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

namespace PopulateDataSet
{

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

            // Modify the following string to correctly connect to your SQL Server.
            sConnectionString = "Password=;User ID=sa;"
                + "Initial Catalog=pubs;"
                + "Data Source=(local)";

            SqlConnection objConn
                = new SqlConnection(sConnectionString);
            objConn.Open();

            // Create an instance of a DataAdapter.
            SqlDataAdapter daAuthors 
                = new SqlDataAdapter("Select * From Authors", objConn);

            // Create an instance of a DataSet, and retrieve 
            // data from the Authors table.
            DataSet dsPubs = new DataSet("Pubs");
            daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
            daAuthors.Fill(dsPubs,"Authors"); 
            //****************
            // BEGIN ADD CODE 
            // Create a new instance of a DataTable.
            DataTable tblAuthors;
            tblAuthors = dsPubs.Tables["Authors"];

            DataRow drCurrent;
            // Obtain a new DataRow object from the DataTable.
            drCurrent = tblAuthors.NewRow();

            // Set the DataRow field values as necessary.
            drCurrent["au_id"] = "993-21-3427";
            drCurrent["au_fname"] = "George";
            drCurrent["au_lname"] = "Johnson";
            drCurrent["phone"] = "800 226-0752";
            drCurrent["address"] = "1956 Arlington Pl.";
            drCurrent["city"] = "Winnipeg";
            drCurrent["state"] = "MB";
            drCurrent["contract"] = 1;

            // Pass that new object into the Add method of the DataTable.
            tblAuthors.Rows.Add(drCurrent);
            Console.WriteLine("Add was successful, Click any key to continue!!");
            Console.ReadLine();

            // END ADD CODE   
            //*****************
            // BEGIN EDIT CODE 

            drCurrent = tblAuthors.Rows.Find("213-46-8915");
            drCurrent.BeginEdit();
            drCurrent["phone"] = "342" + drCurrent["phone"].ToString().Substring(3);
            drCurrent.EndEdit();
            Console.WriteLine("Record edited successfully, Click any key to continue!!");
            Console.ReadLine();
			
            // END EDIT CODE   
            //*****************
            // BEGIN SEND CHANGES TO SQL SERVER 

            SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);
            daAuthors.Update(dsPubs, "Authors");
            Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
            Console.ReadLine();
			
            // END SEND CHANGES TO SQL SERVER 
            //*****************
            //BEGIN DELETE CODE 

            drCurrent = tblAuthors.Rows.Find("993-21-3427");
            drCurrent.Delete();
            Console.WriteLine("SRecord deleted successfully, Click any key to continue!!"); 
            Console.ReadLine();
       
            //END DELETE CODE  
            //*****************
            // CLEAN UP SQL SERVER
            daAuthors.Update(dsPubs, "Authors");
            Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
            Console.ReadLine();         	
			
        }
    }
}
				

Tham kh?o

? bi?t thm chi ti?t v? vi?c s? d?ng ADO.NET, s? li?u ?i t?ng, v SQL, gh thm Web site sau c?a Microsoft:
L?n vo truy c?p d? li?u (m?t c?t MSDN ti?ng ni )
http://msdn2.Microsoft.com/en-US/Library/ms810288.aspx
ADO.NET cho cc l?p tr?nh vin ADO
http://MSDN.Microsoft.com/en-US/Library/ms973217.aspx
MSDN tr?c tuy?n Trung tm nh pht tri?n .NET
http://msdn2.Microsoft.com/en-US/netframework/default.aspx

Thu?c tnh

ID c?a bi: 307587 - L?n xem xt sau cng: 29 Thang Mi 2013 - Xem xt l?i: 3.0
p d?ng
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
T? kha:
kbhowtomaster kbsqlclient kbsystemdata kbmt KB307587 KbMtvi
My d?ch
QUAN TRONG: Bai vit nay c d?ch b?ng phn mm dich thu?t cua Microsoft va co th c Cng ng Microsoft chinh sa l?i thng qua cng ngh CTF thay vi mt bin dich vin chuyn nghip. Microsoft cung c?p cc bi vi?t ?c c? bin d?ch vin v ph?n m?m d?ch thu?t th?c hi?n v c?ng ?ng ch?nh s?a l?i ? 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 nhi?u ngn ng? Tuy nhin, bi vi?t do my d?ch hoc thm chi cng ng chinh sa sau khng ph?i lc no c?ng hon h?o. Cc bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, 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.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny: 307587

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