string sql="update UserGroupTbl set GroupPublishBool="+myCommand.Parameters["@GroupInputBool"].Value;
sql=sql+",GroupInputBool="+myCommand.Parameters["@GroupPublishBool"].Value;
sql=sql+" where UserGroupId='"+myCommand.Parameters["@UserGroupId"].Value+"'";
改为
string sql="update UserGroupTbl set GroupPublishBool=@GroupInputBool;
sql=sql+",GroupInputBool=@GroupPublishBool;
sql=sql+" where UserGroupId=@UserGroupId;
sql=sql+",GroupInputBool="+myCommand.Parameters["@GroupPublishBool"].Value;
sql=sql+" where UserGroupId='"+myCommand.Parameters["@UserGroupId"].Value+"'";
改为
string sql="update UserGroupTbl set GroupPublishBool=@GroupInputBool;
sql=sql+",GroupInputBool=@GroupPublishBool;
sql=sql+" where UserGroupId=@UserGroupId;
sql=sql+",GroupInputBool="+myCommand.Parameters["@GroupPublishBool"].Value;
sql=sql+" where UserGroupId='"+myCommand.Parameters["@UserGroupId"].Value+"'";此时你的myCommand.Parameters["@GroupPublishBool"].Value存的是bool值,其实编译时就会提示错误,因为它无法显示的转换为string类型
只是你可能没有注意,而且这时myCommand.Parameters["@GroupPublishBool"].Value应该呈现出字串1而不是true,完成你的sql构成后别先执行,显示在页面上看看先
我想问的是
你构造了一次myCommand
运行了一次myCommand
构造时赋值一次commandtext
后来赋值一次myCommand
其中第一次需要paramteres
后一次不需要
为什么要这样
构造完成,参数配置完成已不需要最后一步
或无需构造时使用commandtext参数,也无需构造配置其parameters
直接使用纯文本的commandtext亦可
String updateCmd="update UserGroupTbl set GroupPublishBool=@GroupPublishBool,GroupInputBool=@GroupInputBool where UserGroupId=@UserGroupId";
SqlCommand myCommand = new SqlCommand(updateCmd,sqlConn);
myCommand.Parameters.Add(new SqlParameter("@UserGroupId", SqlDbType.VarChar,50));
myCommand.Parameters.Add(new SqlParameter("@GroupPublishBool", SqlDbType.Bit,1));
myCommand.Parameters.Add(new SqlParameter("@GroupInputBool", SqlDbType.Bit,1)); myCommand.Parameters["@UserGroupId"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex];
int k=e.Item.ItemIndex;
CheckBox ck=(CheckBox)(DataGrid1.Items[e.Item.ItemIndex].FindControl("CheckBox1"));
if(ck!=null)
{
if(ck.Checked)
myCommand.Parameters["@GroupPublishBool"].Value="1";
else
myCommand.Parameters["@GroupPublishBool"].Value="0";
} CheckBox ck1=(CheckBox)(DataGrid1.Items[e.Item.ItemIndex].FindControl("CheckBox2")); if(ck1!=null)
{
if(ck1.Checked)
myCommand.Parameters["@GroupInputBool"].Value=bool.Parse("1");
else
myCommand.Parameters["@GroupInputBool"].Value=bool.Parse("0");
}
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
DataGrid1.EditItemIndex=-1;
BindGrid();
}
}
而这样也出现类似的问题,所以我想重新构造,因此才会出现blackant2(乔峰)的问题
myCommand.Parameters.Add(new SqlParameter("@GroupInputBool", SqlDbType.Bit,1));改为myCommand.Parameters.Add(new SqlParameter("@GroupPublishBool", SqlDbType.Int,1));
myCommand.Parameters.Add(new SqlParameter("@GroupInputBool", SqlDbType.Int,1));
myCommand.Parameters["@GroupInputBool"].Value=bool.Parse("1");
else
myCommand.Parameters["@GroupInputBool"].Value=bool.Parse("0");改为myCommand.Parameters["@GroupInputBool"].Value=1;
else
myCommand.Parameters["@GroupInputBool"].Value=0;这样可以