如何取得在DataGridView中修改的数据,指的是直接在表中把原来DataGridView填充的数据做修改后保存到数据库。
解决方案 »
- 关于验证码,不太懂里面那个onclick的那段话,求解
- ★★★★★在资源管理其中 ,如何动态增加右键菜单,分不够再加★★★★★
- C#里如何把颜色RGB转换为16进制
- 概率问题:1-25之中随机产生5个数(5个数不相同)100次,找出一种方法,使5个号同时不出的次数为最多
- 有熟悉Orbiter 的人吗,请联系我 我的邮箱是 [email protected]
- 急~!关于C#连接SQL语句问题
- [提问]现在我程序获取了网页的HTML代码,我只想获取里面的Title部分,应该怎样修改呢?
- 急,简单问题
- C#如何调用C++的DLL的结构体数组指针
- 软件换皮肤怎么弄??
- 一个关于窗体控制的问题
- 一个数据库插入的问题,很苦恼,愿得醍醐灌顶,求赐教!
本节演示如何使用 DataSet 对象更新数据库中的数据。还可以使用 SqlCommand 对象直接在数据库中插入、更新和删除数据,记住这一点很重要。如想更好地理解本文,请单击下面的文章编号以查看 Microsoft 知识库中的文章:
314145 (http://support.microsoft.com/kb/314145/) 如何使用 Visual C# .NET 从数据库填充 DataSet 对象
在文章 314145 (http://support.microsoft.com/kb/314145/) 中讲述的一些主题包括:如何从数据库中检索数据并将其传送到 DataSet 中,DataSet 如何独立于数据库以及与数据库的区别。加载 DataSet 后,就可以修改数据了。DataSet 将会跟踪这些更改。可将 DataSet 对象视为从数据库检索出并缓存于内存中的数据。DataSet 对象由一个包含表、关系和约束的集合构成。若要更新 DataSet 并将这些更新发回数据库,请按照下列步骤操作: 1. 打开 Microsoft Visual Studio .NET。
2. 在 Visual C# .NET 中新建控制台应用程序。默认情况下,Visual Studio 创建一个“静态类”和一个空的 Main() 过程。
3. 确保项目包括一个对 System 和 System.Data 命名空间的引用。在 System、System.Data 和 System.Data.SqlClient 命名空间上使用 using 语句,这样,在后面的代码中就不需要从这些命名空间中限定声明。必须在任何其他声明之前使用这些语句。using System;
using System.Data;
using System.Data.SqlClient;
4. 在修改数据并将更改发回数据库之前,必须将该信息加载到 DataSet 中。有关详细过程,请参见: 314145 (http://support.microsoft.com/kb/314145/)。 为避免重复,将不详细提供该步骤中的代码。以下代码中的连接字符串指向位于本地计算机(或正在运行代码的计算机)的 SQL Server。概括来说,先创建一个连接,然后创建一个数据适配器用以将数据填充到 DataSet 中。
注意:在运行此代码之前,必须将 User ID <username> 和 Password <strong password> 更改为正确的值。请确保该用户 ID 具有在数据库中执行此操作所需的适当权限。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. 现在已经加载了数据,您可以对其进行修改。添加行(或记录)有多种方法。该代码示例使用一个三步式过程: a. • 从 DataTable 获取新的 DataRow 对象。
• 根据需要设置 DataRow 字段值。
• 将新的对象传递给 DataTable.Rows 集合的 Add 方法。
在第 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
// BEGIN EDIT CODEdrCurrent = 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. 要用所有这些更改来更新原始数据库,可将 DataSet 传递到 DataAdapter 对象的 Update 方法。不过,在调用 Update 之前,必须先设置 DataAdapter 对象的 InsertCommand、UpdateCommand 和 DeleteCommand 属性。可手动编写 SQL 并用相应的 SqlCommand 对象填充这三个属性,但也可以使用 Visual Studio .NET 自动生成这三个命令。若要在需要时生成所需的命令,必须创建 SqlCommandBuilder 对象的实例并使用该构造函数中的 DataAdapter。如果想使用此方法(在以下代码示例中阐释),您的表必须有主键信息。要访问主键信息,可调用 FillSchema,然后将 DataAdapter 的 MissingSchemaAction 属性设置为 AddWithKey,或在代码中手动设置主键。将以下代码粘贴到 EDIT 代码之后://*****************
// BEGIN SEND CHANGES TO SQL SERVERSqlCommandBuilder 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. 要完全删除一行,可使用 DataRow 对象的 Delete 方法。请注意,Rows 集合包含 Remove 和 RemoveAt 两个方法,它们似乎删除了行,但实际上只是将行从集合中移除。只有 Delete 方法才会将删除结果发回源数据库中。将以下代码粘贴到 SEND CHANGES TO SQL SERVER 代码之后://*****************
//BEGIN DELETE CODEdrCurrent = tblAuthors.Rows.Find("993-21-3427");
drCurrent.Delete();
Console.WriteLine("Record deleted successfully, Click any key to continue!!");
Console.ReadLine();//END DELETE CODE
9. 将这些更改发送到 SQL Server 以移除早先添加的记录。将以下代码粘贴到 DELETE 代码之后://*****************
// CLEAN UP SQL SERVER
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
10. 保存项目。
11. 在调试菜单上单击启动运行该项目。请注意,将出现几个消息框,它们指示代码的执行进度并让您能够在执行过程中查看数据的当前状态。 回到顶端 完整代码列表
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(); }
}
}
例如:DataGridView中填充好数据后有一行数据如下:
ID name
01 TOM
我要在TOM上点鼠标,然后修改为JOHN,然后保存到数据库。现在是能修改是能改的,但是怎么把修改保存到数据库,关键不知道怎么取得修改的值。