我有一个包含 3 列的表(EmpId、EmpName、EmpSalary)。我正在使用准备好的语句来使用准备好的语句从该表中检索所有数据。这是我写的...
try
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT EmpId,EmpName,EmpSalary FROM EmpDetails";
SqlParameter paraId = new SqlParameter();
paraId.ParameterName = "@id";
paraId.SqlDbType = SqlDbType.Int;
paraId.Size = 32;
SqlParameter paraName = new SqlParameter();
paraName.ParameterName = "@name";
paraName.SqlDbType = SqlDbType.VarChar;
paraName.Size = 50;
SqlParameter paraSal = new SqlParameter();
paraSal.ParameterName = "@sal";
paraSal.SqlDbType = SqlDbType.Decimal;
paraSal.Precision = 7;
paraSal.Scale = 2;
cmd.Parameters.Add(paraId);
cmd.Parameters.Add(paraName);
cmd.Parameters.Add(paraSal);
con.Open();
cmd.Prepare();
SqlDataReader dr = cmd.ExecuteReader();
string str = "";
while(dr.Read())
{
string id = dr.GetInt32(0).ToString();
string name = dr.GetString(1);
string sal = dr.IsDBNull(2) ? "is null" : dr.GetDecimal(2).ToString();
str += id + "\t" + name + "\t" + sal + "\n";
}
MessageBox.Show(str);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
但我收到如下错误:“参数化查询需要参数@id,但未提供。”
我在这里犯了什么错误..?