private DataSet ds=new DataSet();//定义
private void Form1_Load(object sender, System.EventArgs e)
{
String cmdSQL = "select from * t_user";
SqlDataAdapter cmd = new SqlDataAdapter(cmdSQL, Conn);
cmd.Fill(ds,"user");
}
//以下删除选定的行
private void btnDelete_Click(object sender, System.EventArgs e)
{
BindingManagerBase bm=this.BindingContext[this.myGrid.DataSource];
DataRowView drv=(DataRowView)bm.Current;
drv.Delete();
ds.Tables["user"].AcceptChanges();
}
但每次程序重新运行以后,都没有删除记录。
private void Form1_Load(object sender, System.EventArgs e)
{
String cmdSQL = "select from * t_user";
SqlDataAdapter cmd = new SqlDataAdapter(cmdSQL, Conn);
cmd.Fill(ds,"user");
}
//以下删除选定的行
private void btnDelete_Click(object sender, System.EventArgs e)
{
BindingManagerBase bm=this.BindingContext[this.myGrid.DataSource];
DataRowView drv=(DataRowView)bm.Current;
drv.Delete();
ds.Tables["user"].AcceptChanges();
}
但每次程序重新运行以后,都没有删除记录。
要在ds.Tables["user"].AcceptChanges();之前调用SqlDataAdapter的Update()方法更新到数据库中
并在之前还要做数据库中的表和dataSet中表的映射
private DataTable dataT;
private SqlDataAdapter adp;
private SqlCommandBuilder sqlBuilder = new SqlCommandBuilder();try
{
if(dataT.Rows.Count != 0)
{
if(dataGrid1.VisibleRowCount - 1 > count)
{
adp.Update(dataT);
count = dataT.Rows.Count;
}
else
{
if(MessageBox.Show("真的要删除当前记录吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information )
== DialogResult.OK )
{
string deletecmd;
SqlConnection conn = new SqlConnection (strConAll);
SqlCommand cmd ;
deletecmd = " Delete From schedule_info Where time_id = CONVERT(smalldatetime,'" + dataT.Rows[dataGrid1.CurrentCell.RowNumber][0].ToString() + "') ";
conn.Open ();
cmd = new SqlCommand(deletecmd,conn);
cmd.ExecuteNonQuery();
conn.Close();
adp.Update(dataset,"schedule_info");
dataset.Clear();
adp.Fill(dataset,"schedule_info");
count = dataT.Rows.Count;
}
}
}
else
MessageBox.Show("无记录可删除!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
}
catch(Exception ex){ MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information ); }
}
表的映射怎么做?