1

当我尝试执行此代码时收到以下错误。但我将它添加到我的命令中。有人可以指出忽略的步骤吗?谢谢。

过程或函数“usps_getContactDetails”需要参数“@aspContactID”,但未提供该参数。

SqlConnection conn = new SqlConnection(GetConnString()); SqlCommand cmd = new SqlCommand("usps_getContactDetails", conn);

        SqlParameter parmContactID = new SqlParameter("@aspContactID", Convert.DBNull);
        cmd.Parameters.Add(parmContactID);
        parmContactID.Direction = ParameterDirection.Input;

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        conn.Open();
        DataSet cusDS = new DataSet();
        da.Fill(cusDS, "Contacts");
4

2 回答 2

0

在执行 aSqlCommand并调用存储过程时,您需要将您的隐式设置SqlCommand为 a StoredProcedure

using(SqlConnection con = new SqlConnection(""))
{
  //Set up your command
  SqlCommand cmd = new SqlCommand("[Procedure]", con);
  cmd.CommandType = CommandType.StoredProcedure;
  //Add your parameters
  cmd.Parameters.AddWithValue("@aspContactID", "");
  //Declare your data adapter
  SqlDataAdapter sda = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  sda.Fill(ds, "Contacts");
}

按照上面的格式,你应该没问题。您的程序无法正常工作有两个原因之一,您错过了在这种情况下使您的代码工作的代码行,cmd.CommandType = CommandType.StoredProcedure;或者因为您的参数是DBNull程序表示它没有对该参数的任何识别。如果您在存储过程中有一个可以为空或为空的参数,请执行以下操作:

Create Procedure [dbo].[Example]

@Test as Varchar(100) = ''

As
于 2012-12-14T14:27:10.277 回答
0
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void OpenCon()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbPrepConnectionString"].ConnectionString.ToString());
        try
        {
            con.Open();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }



    private void SubmitData()
    {
        OpenCon();
        string sp = "sp_InsertRecord";
        cmd = new SqlCommand(sp, con);

        cmd.CommandType = CommandType.StoredProcedure;
        //add parameters...
        cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 50));
        cmd.Parameters.Add(new SqlParameter("@UserId", SqlDbType.Int));
        cmd.Parameters.Add (new SqlParameter ("@ProductName",SqlDbType .VarChar,50));
        cmd.Parameters.Add(new SqlParameter("@Price", SqlDbType.Money));

        //set paarameters....
        cmd.Parameters["@Name"].Value = txtName.Text.ToString();
        cmd.Parameters["@UserId"].Value = txtUserId.Text.ToString();
        cmd.Parameters["@ProductName"].Value = txtProductName.Text.ToString();
        cmd.Parameters["@Price"].Value = txtPrice.Text.ToString();
        cmd.ExecuteNonQuery();
        lblMessage.Text = "data inserted successfully";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SubmitData();
    }

    private void FindData()
    {
        OpenCon();
        string s = "sp_FindRecord";
        cmd = new SqlCommand(s, con);
        cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int));
        cmd.Parameters["@Id"].Value = txtName.Text.ToString();
        cmd.CommandType = CommandType.StoredProcedure;
        ad = new SqlDataAdapter(cmd);
        ds = new DataSet();
        ad.Fill(ds);
        dt = ds.Tables[0];
        currow = 0;

        FillControls();

    }

    private void FillControls()
    {
        txtOrderId.Text = dt.Rows[currow].ItemArray[0].ToString();
        txtUserId.Text = dt.Rows[currow].ItemArray[1].ToString();
        txtProductName.Text = dt.Rows[currow].ItemArray[2].ToString();
        txtPrice.Text = dt.Rows[currow].ItemArray[3].ToString();
    }

    protected void btnFind_Click(object sender, EventArgs e)
    {
        FindData();
    }

}

}'

于 2013-05-16T08:21:45.877 回答