日期:2014-05-20  浏览次数:20836 次

在C#.net 中设置数据库变量??请高手多多赐教,小女子先谢过了
private   void   frmaddbooks_Load(object   sender,   System.EventArgs   e)
{


objsqlconn=new   SqlConnection   ( "workstation   id=USER;packet   size=4096;integrated   security=SSPI;data   source=USER;persist   security   info=False;initial   catalog=flights ");
objsqlconn.Open   ();
objsqldataadapter=new   SqlDataAdapter   ();
objsqldataadapter.SelectCommand   =new   SqlCommand   ( "select   *   from   books ",objsqlconn);
objdataset=new   DataSet   ();
objsqldataadapter.Fill   (objdataset, "books ");
this.dataGrid1   .SetDataBinding   (objdataset, "books ");
string   insmd= "insert   into   books   values(@bookaccesscode,@name,@author,@proice,@qty) ";
objsqldataadapter.InsertCommand     =new   SqlCommand   (insmd,objsqlconn);


objsqlparameter=objsqldataadapter.SelectCommand   .Parameters   .Add   ( "@bookaccesscode ",SqlDbType.VarChar   );
objsqlparameter.SourceColumn   = "bookaccesscode ";
objsqlparameter.SourceVersion   =DataRowVersion.Current   ;


string   insmd= "insert   into   books   values(@bookaccesscode,@name,@author,@proice,@qty) ";
中(@bookaccesscode,@name,@author,@proice,@qty怎么在c#.net中定义变量,因为当运行后系统老是提示必须申明变量(@bookaccesscode,@name,@author,@proice,@qty,但又不知在c#中怎么申明?请各位帮忙!!!谢谢了

------解决方案--------------------
sql = "Update PGSubMenu set mmId=@mmId,smName=@smName,Tooltips=@Tooltips,isUsed=@isUsed,SerialNo=@SerialNo,Description=@Description where sId=@sId ";
cmd.Parameters.Add( "@sId ", SqlDbType.NChar, 40, "sId ");
cmd.Parameters[ "@sId "].Value = sId;
}
//cmd = new SqlCommand(sql, conn);
cmd.CommandText = sql;
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;

cmd.Parameters.Add( "@mmId ", SqlDbType.NChar, 40, "mmId ");
cmd.Parameters.Add( "@smName ", SqlDbType.NChar, 100, "smName ");
cmd.Parameters.Add( "@Tooltips ", SqlDbType.NChar, 100, "Tooltips ");
cmd.Parameters.Add( "@isUsed ", SqlDbType.Bit, 1, "isUsed ");
cmd.Parameters.Add( "@SerialNo ", SqlDbType.Int, 4, "SerialNo ");
cmd.Parameters.Add( "@Description ", SqlDbType.NChar, 100, "Description ");

cmd.Parameters[ "@mmId "].Value = mmId;
cmd.Parameters[ "@smName "].Value = smName;
cmd.Parameters[ "@Tooltips "].Value = Tooltips;
cmd.Parameters[ "@isUsed "].Value = isUsed;
cmd.Parameters[ "@SerialNo "].Value = SerialNo;
cmd.Parameters[ "@Description "].Value = Description;
------解决方案--------------------
你下边那不有声明参数的语句嘛,跟那个一样,添加到insertCommand.Parameters集合里就可以了

objsqlparameter=objsqldataadapter.SelectCommand .Parameters .Add ( "@bookaccesscode ",SqlDbType.VarChar );
objsqlparameter.SourceColumn = "bookaccesscode ";
objsqlparameter.SourceVersion =DataRowVersion.Current ;