private void btnUpdate_Click(object sender, System.EventArgs e)
{
SqlConnection thisConnection=new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;"+"Initial Catalog=student");
thisConnection.Open();//连接数据库
SqlDataAdapter da=new SqlDataAdapter();
DataTable dt=new DataTable();
da.Fill(dt);
this.dataGrid1.DataSource=dt;
DataRow[] rr=dt.Select("","",DataViewRowState.ModifiedCurrent);
if(rr==null || rr.Length<=0)
{
MessageBox.Show("没有需要更改的数据!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
SqlConnection con=new
SqlCon(Main.constr).GetCon();
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandText="update Test set [学号]=?,[姓名]=?,[性别]=?,[出生日期]=?,[籍贯]=?,[政治面貌]=?,[民族]=?,[毕业时间]=?,[就业情况]=?,[就业单位]=?where[学号]=?and[姓名]=?and[性别]=?and[出生日期]=?and[籍贯]=?and[政治面貌]=?and[民族]=?and[毕业时间]=?and[就业情况]=?and[就业单位]=?";
cmd.Parameters.Add("new学号",
SqlType.Char,50,"学号");
cmd.Parameters.Add("new姓名",
SqlType.Char,50,"姓名");
cmd.Parameters.Add("new性别",
SqlType.Char,2,"性别");
cmd.Parameters.Add("new出生日期",
SqlType.Char,100,"出生日期");
cmd.Parameters.Add("new籍贯",
SqlType.Char,50,"籍贯");
cmd.Parameters.Add("new政治面貌",
SqlType.Char,50,"政治面貌");
cmd.Parameters.Add("new民族",
SqlType.Char,2,"民族");
cmd.Parameters.Add("new毕业时间",
SqlType.Char,100,"毕业时间");
cmd.Parameters.Add("new就业情况",
SqlType.Char,2,"就业情况");
cmd.Parameters.Add("new就业单位",
SqlType.Char,100,"就业单位");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old学号",SqlType.Char,50,"学号");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old姓名",SqlType.Char,50,"姓名");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old性别",SqlType.Char,2,"性别");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old出生日期",SqlType.Char,100,"出生日期");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old籍贯",SqlType.Char,50,"籍贯");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old政治面貌",SqlType.Char,50,"政治面貌");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old民族",SqlType.Char,2,"民族");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old毕业时间",SqlType.Char,100,"毕业时间");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old就业情况",SqlType.Char,2,"就业情况");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("old就业单位",SqlType.Char,100,"就业单位");
para.SourceVersion=DataRowVersion.Original;
SqlDataAdapter
dv=new SqlDataAdapter();
dv.UpdateCommand=cmd;
try
{
da.Update(dt.Select("","",DataViewRowState.ModifiedCurrent));
}
catch(SqlException er)
{
MessageBox.Show(er.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
MessageBox.Show("已成功修改数据!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
我好笨啊,套用都不会用啊
红色的出错,救命啊
[此贴子已经被作者于2006-8-8 15:33:07编辑过]