怎么样用DATASET往数据库添加数据,不用SQL语句!

sequelbo 2003-07-31 08:55:15
怎么样用DATASET往数据库添加数据,不用SQL语句!
...全文
70 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dahuzizyd 2003-07-31
  • 打赏
  • 举报
回复
也可以这样,使用CommandBuider,它会给你的Adapter生成相应的InsertCommand,UpdateCommand :
private void button1_Click(object sender, System.EventArgs e)
{
OleDbConnection conn = new OleDbConnection("YourConnectionString");

OleDbCommand cmd = new OleDbCommand("select * from department",conn);
OleDbDataAdapter adp = new OleDbDataAdapter();
adp.SelectCommand = cmd;

OleDbCommandBuilder builder = new OleDbCommandBuilder(adp);
DataSet ds = new DataSet();
conn.Open();
adp.Fill(ds,"department");

DataRow dr = ds.Tables[0].NewRow();
dr[0] = 100022;
dr[1] = 0;
r[2] = "aaa";
ds.Tables[0].Rows.Add(dr);

adp.Update(ds,"department");
}
详细的使用方法可以参考帮助里的OleDbCommandBuilder类
oh_love 2003-07-31
  • 打赏
  • 举报
回复
不可能吧!能用DataSet直接更新数据库
雪狼1234567 2003-07-31
  • 打赏
  • 举报
回复
用DataSet的话,只有一种方法如下:
首先取得数据,放到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");

然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
然后进行数据的操作如:
增加:
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();
}
相关推荐
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2003-07-31 08:55
社区公告

让您成为最强悍的C#开发者