当运行到 da2.Update(ds2, "baobiao"); 时出现如下错误提示:必须声明变量 '@RQ' ,如何解决?谢谢!
protected void Button1_Click1(object sender, EventArgs e)
{
String MyText="A";
if (RadioButtonList1.Text.ToString() == "工程报表" || RadioButtonList1.Text.ToString() == "实情汇报")
{
MyText = "工程报表及实情汇报";
}
if (RadioButtonList1.Text.ToString() == "工地检查通知" || RadioButtonList1.Text.ToString() == "工地检查通报")
{
MyText = "工地检查通知及通报";
}
SqlConnection conn2 = new SqlConnection("data source=(local);database=****;user=****; password=********"); //创建连接对象
SqlDataAdapter da2 = new SqlDataAdapter();
//适配器对象的查询命令
SqlCommand cmdSel = new SqlCommand("select * from baobiao", conn2); //创建DataAdapter(数据适配器)
da2.SelectCommand = cmdSel;
//适配器对象的更新命令
SqlCommand cmdUpdate = new SqlCommand("update baobiao set RQ=@RQ,WJLX=@WJLX,WJLJ=@WJLJ where ID = @ID", conn2);
SqlParameter paramID = new SqlParameter("@ID", SqlDbType.Int);
paramID.SourceColumn = "ID"; //数据值对应的字段
paramID.SourceVersion = DataRowVersion.Original; //数据值将使用初始版本的数值
cmdUpdate.Parameters.Add(paramID);
cmdUpdate.Parameters.Add("@RQ", SqlDbType.NChar, 50, "RQ");
cmdUpdate.Parameters.Add("@WJLX", SqlDbType.Int, 4, "WJLX");
cmdUpdate.Parameters.Add("@WJLJ", SqlDbType.NChar, 150, "WJLJ");
da2.UpdateCommand = cmdUpdate; //
//适配器对象的添加命令
SqlCommand cmdInsert = new SqlCommand("insert into baobiao(RQ,WJLX,WJLJ) values (@RQ,@WJLX,@WJLJ)", conn2);
cmdInsert.Parameters.Add("@ID", SqlDbType.Int, 4, "ID");
cmdUpdate.Parameters.Add("@RQ", SqlDbType.NChar, 50, "RQ");
cmdUpdate.Parameters.Add("@WJLX", SqlDbType.Int, 4, "WJLX");
cmdUpdate.Parameters.Add("@WJLJ", SqlDbType.NChar, 150, "WJLJ");
da2.InsertCommand = cmdInsert;
DataSet ds2 = new DataSet(); //创建DataSet(数据集)
da2.Fill(ds2, "baobiao"); //使用数据适配器填充数据集
//添加数据
DataRow addrow = ds2.Tables[0].NewRow();
addrow[1] = DropDownList1.Text + "年" + DropDownList2.Text + "月";
addrow[2] = RadioButtonList1.SelectedIndex + 1;
addrow[3] = "files/" + DropDownList1.Text + "年/" + MyText + "/" + FileUpload1.FileName;
ds2.Tables[0].Rows.Add(addrow);
GridView1.DataSource = ds2.Tables["baobiao"]; //显示,绑定
GridView1.DataBind();
da2.Update(ds2, "baobiao");
}
}
[此贴子已经被作者于2007-3-16 10:56:53编辑过]