ADO.NET更新datatable到数据库的问题

ahljxwy 2012-01-09 10:09:36
        public Boolean UpdateDataTable(DataTable dt, string strSQL)
//dt 是一个DataTable;strSQL是之前查询这个DataTable时所用的SQL查询语句
//注:这个DataTable查出来后跟数据库的连接就断开了,这里要将这个DataTable更新到数据库里需要重新建立连接
{

SqlConnection conn = commSql.ReturnDbConn();

try
{

SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
SqlCommandBuilder cda = new SqlCommandBuilder(da);
da.Update(dt);
return true;

}
catch (Exception ex)
{
throw ex;
return false;
}
finally
{
commSql.DisposeConn(conn);
}
}


各位,帮我看看吧,这是我写的一个函数,放在服务端的,用于更新传过来的datatable
帮我看看有没有什么问题吧,多谢!
...全文
122 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
风骑士之怒 2012-01-10
  • 打赏
  • 举报
回复
剖析到底层,不也是遍历dt,构造sql,链接数据库并执行SQL
艾米01 2012-01-10
  • 打赏
  • 举报
回复
用存储过程多好
ahljxwy 2012-01-10
  • 打赏
  • 举报
回复
继续求解.......
风骑士之怒 2012-01-09
  • 打赏
  • 举报
回复
断开后,可以以这种形式更新的。。

查看一个winform例子:


public partial class Form1 : Form
{
private DataTable dt = new DataTable();
private String sql = "select * from school";
public Form1()
{
InitializeComponent();
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
UpdateDataTable(dt, sql);
}

private void UpdateDataTable(DataTable dt, string strSQL)
{
using (SqlConnection conn = new SqlConnection("server=.;user id=sa;pwd=111;database=jxwmw_guoxue2"))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
SqlCommandBuilder cbr = new SqlCommandBuilder(da);
da.Update(dt);
}
}

private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("server=.;user id=sa;pwd=111;database=jxwmw_guoxue2"))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
}

DataView dv = dt.DefaultView;
dataGridView1.DataSource = dv;
}
}
风骑士之怒 2012-01-09
  • 打赏
  • 举报
回复
断开后,可以以这种形式更新的。。

查看一个winform例子:


public partial class Form1 : Form
{
private DataTable dt = new DataTable();
private String sql = "select * from school";
public Form1()
{
InitializeComponent();
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
UpdateDataTable(dt, sql);
}

private void UpdateDataTable(DataTable dt, string strSQL)
{
using (SqlConnection conn = new SqlConnection("server=.;user id=sa;pwd=111;database=jxwmw_guoxue2"))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
SqlCommandBuilder cbr = new SqlCommandBuilder(da);
da.Update(dt);
}
}

private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("server=.;user id=sa;pwd=111;database=jxwmw_guoxue2"))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
}

DataView dv = dt.DefaultView;
dataGridView1.DataSource = dv;
}
}
jiuhexuan 2012-01-09
  • 打赏
  • 举报
回复
Update不能这么用吧

一般是取出的dataset,修改数据,用取ds的dataadpater的更新

你这没有关联
ahljxwy 2012-01-09
  • 打赏
  • 举报
回复
还有其它解释吗?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧