使用ASP.net和c#的存储过程从SQL Server检索数据

时间:2015-08-03 08:57:32

标签: c# sql asp.net sql-server

我有一个表单,我想从sql表中检索数据并在表单的字段中显示它,具体取决于我在url中输入的?id,但我总是收到此错误:< / p>

  

程序或功能&#39; GetAppForm&#39;期望参数&#39; @ id&#39;,这是   没提供。

注意:GetAppForm是存储过程。

这是我的代码,请帮助我:

try
{
    if (String.IsNullOrEmpty(Request.QueryString["id"]))
    {
        sqlConn.Open();

        using (SqlCommand cmd = new SqlCommand("GetAppForm", sqlConn))
        {
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter id = cmd.Parameters.Add("@id", SqlDbType.Int);                    
            id.Direction = ParameterDirection.Input;
            id.Value = Request.QueryString["id"];

            SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            while (dataReader.Read())
            {
                OwnerField.Text = dataReader["Owner"].ToString();
                OdBookNoField.Text = dataReader["OD"].ToString();
                PdLocField.Text = dataReader["pd"].ToString();
                StatementNoField.Text = dataReader["Statmnt"].ToString();
                ApplicationNoField.Text = dataReader["AppNo"].ToString();
                AppDateField.Text = dataReader["AppDate"].ToString();
                areaField.Text = dataReader["Area"].ToString();
                areaNoField.Text = dataReader["AreaNo"].ToString();
                blockNoField.Text = dataReader["BlockNo"].ToString();
                streetNoField.Text = dataReader["StreetNo"].ToString();
            }
        }
    }
}
catch (Exception ex)
{
    HttpContext.Current.Response.Write("No Connection!!");
}
finally
{
    sqlConn.Close();
}

3 个答案:

答案 0 :(得分:0)

更改

if (String.IsNullOrEmpty(Request.QueryString["id"]))

if (!String.IsNullOrEmpty(Request.QueryString["id"]))

答案 1 :(得分:0)

我想你只是忘了否定String.IsNullOrEmpty条件:

 try
        {
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {

请注意,您的代码很容易被注射。

答案 2 :(得分:0)

try
            {
                if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    sqlConn.Open();
                    using (SqlCommand cmd = new SqlCommand("GetAppForm", sqlConn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@id", Request.QueryString["id"]);                       
                        SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        //SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.SingleRow);
                        while (dataReader.Read())
                        {
                            OwnerField.Text = dataReader["Owner"].ToString();
                            OdBookNoField.Text = dataReader["OD"].ToString();
                            PdLocField.Text = dataReader["pd"].ToString();
                            StatementNoField.Text = dataReader["Statmnt"].ToString();
                            ApplicationNoField.Text = dataReader["AppNo"].ToString();
                            AppDateField.Text = dataReader["AppDate"].ToString();
                            areaField.Text = dataReader["Area"].ToString();
                            areaNoField.Text = dataReader["AreaNo"].ToString();
                            blockNoField.Text = dataReader["BlockNo"].ToString();
                            streetNoField.Text = dataReader["StreetNo"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("No Connection!!");
            }
            finally
            {
                sqlConn.Close();
            }