这是我用配置sqlcommand对象的方法
SqlCommand PrepareCommand(SqlConnection conn, string cmdtext, SqlParameter[] cmdparas)
{
try
{
SqlCommand comm = new SqlCommand(cmdtext, conn); if (cmdparas != null)
{
foreach (SqlParameter sp in cmdparas)
{
comm.Parameters.Add(sp);
}
}
return comm;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
} }下面是使用
string id = datagvlist.Rows[datagvlist.CurrentCell.RowIndex].Cells[0].Value.ToString();//获得DataGridView中的值 SqlConnection conn = new SqlConnection("server=XU;Integrated Security=true;DataBase=guanlidb"); SqlParameter[] sps = new SqlParameter[1]; SqlParameter sp = new SqlParameter("@id", SqlDbType.Int, 3, id);
sps[0] = sp;
SqlCommand comm = PrepareCommand(conn, "delete from yuangong where id=@id", sps);
conn.Open(); comm.ExecuteNonQuery(); conn.Close();错误提示 未提供@id参数 ,不知道怎么加得上去,帮忙解决一下,谢谢了
如果可以的话,请帮忙写一个完整的例子,谢谢了
SqlCommand PrepareCommand(SqlConnection conn, string cmdtext, SqlParameter[] cmdparas)
{
try
{
SqlCommand comm = new SqlCommand(cmdtext, conn); if (cmdparas != null)
{
foreach (SqlParameter sp in cmdparas)
{
comm.Parameters.Add(sp);
}
}
return comm;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
} }下面是使用
string id = datagvlist.Rows[datagvlist.CurrentCell.RowIndex].Cells[0].Value.ToString();//获得DataGridView中的值 SqlConnection conn = new SqlConnection("server=XU;Integrated Security=true;DataBase=guanlidb"); SqlParameter[] sps = new SqlParameter[1]; SqlParameter sp = new SqlParameter("@id", SqlDbType.Int, 3, id);
sps[0] = sp;
SqlCommand comm = PrepareCommand(conn, "delete from yuangong where id=@id", sps);
conn.Open(); comm.ExecuteNonQuery(); conn.Close();错误提示 未提供@id参数 ,不知道怎么加得上去,帮忙解决一下,谢谢了
如果可以的话,请帮忙写一个完整的例子,谢谢了
SqlParameter[] parameters = {
new SqlParameter("@ID", SqlDbType.Int,4)
};
sps=new SqlParameter("@id", SqlDbType.Int, 4);
sps[0].value=id;
SqlCommand comm = PrepareCommand(conn, "delete from yuangong where id=@id", sps);
......
sps[0] = sp;
===>SqlParameter[] sps = new SqlParameter[]{
new SqlParameter("@id",id)
};
SqlParameter[] sps = new SqlParameter[]{
new SqlParameter("@id",int.Parse(id))
};