0

我有一个包含 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,但未提供。”

我在这里犯了什么错误..?

4

2 回答 2

1

请使用cmd.Parameters.AddWithValue(). 不推荐使用cmd.Parameters.Add()。谷歌它的用途:-)

于 2013-01-01T06:28:33.120 回答
0

我检查了您的代码,您需要提供如下参数值。

        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;

            //i assume you forgot to setup parameter values.

            paraId.Value = 1;
            paraName.Value = "thisName";
            paraSal.Value = 343;


            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();
        }
于 2013-01-01T07:23:11.643 回答