如何修改dataset中的表中的数据,并通过data adapter将修改提交的数据源? seehttp://www.codeproject.com/cs/database/relationaladonet.asp 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.c-sharpcorner.com/Database/PlayingWithDataGridTA.asp SqlDataAdapter catDA = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn); catDA.UpdateCommand = new SqlCommand("UPDATE Categories SET CategoryName = @CategoryName " + "WHERE CategoryID = @CategoryID" , nwindConn);catDA.UpdateCommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");SqlParameter workParm = catDA.UpdateCommand.Parameters.Add("@CategoryID", SqlDbType.Int);workParm.SourceColumn = "CategoryID";workParm.SourceVersion = DataRowVersion.Original;DataSet catDS = new DataSet();catDA.Fill(catDS, "Categories"); DataRow cRow = catDS.Tables["Categories"].Rows[0];cRow["CategoryName"] = "New Category";catDA.Update(catDS); 首先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");conn.Open();System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);dt = new System.Data.DataSet();da.Fill(dt,"student");然后绑定数据集和DataGridDataGrid.SetDataBinding(dt,"student");如果需要,可以绑定TextBox来作录入,而用DataGrid显示this.textBox16.DataBindings.Add("Text",dt,"student.stuno");然后进行数据的操作如:增加:this.BindingContext[dt,"student"].AddNew();删除:this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);最后把结果写回数据库:// sqlInsertCommand1// this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)"; this.sqlInsertCommand1.Connection = this.conn;this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));// // sqlUpdateCommand1// this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";this.sqlUpdateCommand1.Connection = this.conn;this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null)); // sqlDeleteCommand1// this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";this.sqlDeleteCommand1.Connection = this.conn;this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null)); this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;this.sqlDa.InsertCommand = this.sqlInsertCommand1;this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;try{ sqlDa.Update(dt.GetChanges,"student"); return true;}catch(System.Data.SqlClient.SqlException ex){ return false;} finally{ conn.Close();} 我建议你去看一下WWW。WROX。COM出的ADO。NET高级编程,上面的讲的东西很全面的。很详细的 sigh,这么简单一个问题引得这么多高手回复,楼主好大面子哦 剪贴板操作问题,求教高手。。。 按路径建立目录的问题 C# 提示错误:无法加载 DLL“libmx.dll”: 找不到指定的模块 出入mvc战海,各种迷惑和不解 怎样将数据流上传到WCF服务器 如何动态生成CheckedListBox?在winform里面 在DOS界面下,怎么手工编译C#程序啊??(紧急!!紧急!!) 怎样给label添加一个键盘事件? R语言与C# 怎样在菜单的菜单项前加上图标,(包括上下文菜单)急 请问谁有包含全国说有县市名称的数据库,给我一份,500分回报! 关于CurrencyManager的问题
"WHERE CategoryID = @CategoryID" , nwindConn);catDA.UpdateCommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");SqlParameter workParm = catDA.UpdateCommand.Parameters.Add("@CategoryID", SqlDbType.Int);
workParm.SourceColumn = "CategoryID";
workParm.SourceVersion = DataRowVersion.Original;DataSet catDS = new DataSet();
catDA.Fill(catDS, "Categories"); DataRow cRow = catDS.Tables["Categories"].Rows[0];
cRow["CategoryName"] = "New Category";catDA.Update(catDS);
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
如果需要,可以绑定TextBox来作录入,而用DataGrid显示
this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
最后把结果写回数据库:
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{
return false;
}
finally
{
conn.Close();
}