110,533
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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;
}
}
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;
}
}