【问题标题】:Binding gridview using stored procedure使用存储过程绑定gridview
【发布时间】:2014-08-06 06:24:03
【问题描述】:

我有 gridview 并且我在后端使用了存储过程。问题是我没有使用任何文本框或仅标签的gridview。

当我运行我的代码时,它显示一个编译错误,即 spproduct1 需要参数 @id,而该参数预计不会提供。 能否请您修复错误

public partial class WebForm1 : System.Web.UI.Page

{

  string _strsql = string.Empty;

  protected void Page_Load(object sender, EventArgs e)

  {

   string cs = ("Data Source=172.16.6.173;Initial Catalog=servion_hari;User  ID=sa;Password=Servion@123");

   _strsql = "select * from tblproduct where id=@id and Name=@Name and  Description=@Description";

    SqlConnection con = new SqlConnection(cs);

    con.Open();

    SqlDataAdapter da = new SqlDataAdapter("spgetproducts1", con);

    SqlCommand cmd = new SqlCommand();

    SqlParameter id = new SqlParameter("@id", SqlDbType.Int.ToString());

    id.Value = GridView1.ToString();


    da.SelectCommand.CommandType = CommandType.StoredProcedure;


     con.Close();

    DataSet ds = new DataSet();

    da.Fill(ds);

    GridView1.DataSource = ds;

     GridView1.DataBind();


        }
    }
}

【问题讨论】:

  • 参数总是添加到SqlCommand。搜索SqlCommand。

标签: c# asp.net gridview


【解决方案1】:

对于存储过程方式:

        SqlConnection con = new SqlConnection(cs);
        SqlDataAdapter da = new SqlDataAdapter("spgetproducts1", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        //first paramenter: parameter name, second parameter: parameter value of object type
        //using this way you can add more parameters
        da.SelectCommand.Parameters.AddWithValue("@id", GridView1.ToString());
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

问题;你想用 GridView1.ToString() 实现什么?

【讨论】:

  • @hari 你是怎么写这些的:com.Parameters.AddWithValue("@id", yourIdValue); com.Parameters.AddWithValue("@name", yourNameValue); com.Parameters.AddWithValue("@description", yourDescriptionValue);
【解决方案2】:

如果您想为查询中定义的参数添加值,可以使用以下命令:

cmd.Parameters.AddWithValue("@id", GridView1.ToString());

为了更好的实现:

  1. 尝试将所有与数据库相关的函数放在一个单独的文件中,并在您的表单中调用它们。
  2. 在您的数据库上定义一个存储过程并通过 C# 代码调用它。这种使用存储过程的方式很差

已编辑

步骤1: 像这样在数据库中声明您的存储过程:

CREATE PROCEDURE [dbo].[GET_PRODUCTS_SP]
    /*Type of this variables should be their column types*/
    @id int,
    @name varchar(MAX),
    @description varchar(MAX)
AS
BEGIN
    SELECT * FROM [dbo].[tblproduct] 
    WHERE id=@id AND 
          Name=@name AND 
          Description=@description
END

第二步:像这样调用存储过程:

DataTable dt = new DataTable();
String conStr = "Data Source=172.16.6.173;Initial Catalog=servion_hari;User  ID=sa;Password=Servion@123";
SqlConnection con = new SqlConnection(conStr);
SqlCommand com = new SqlCommand("GET_PRODUCTS_SP", con);
com.Parameters.AddWithValue("@id", yourIdValue);
com.Parameters.AddWithValue("@name", yourNameValue);
com.Parameters.AddWithValue("@description", yourDescriptionValue);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
try
{
    con.Open();
    da.Fill(dt);
}
catch (Exception)
{
    throw;
}
finally
{
    if (con.State == ConnectionState.Open)
    con.Close();
}
GridView1.DataSource = dt;
GridView1.DataBind();

https://studio.youtube.com/video/Aqoq_5teKtY/edit?utm_campaign=upgrade&utm_medium=redirect&utm_source=%2Fmy_videos

【讨论】:

  • 我编辑了我的解决方案并建议您使用它来实现您的存储过程:)
  • @hari:你能展示你如何将值传递给参数“@id”、“@name”、“@description”吗?
  • @Dev 你问的是我的SP还是前端编码
  • @hari 是的,来自您的前端。
  • @Dev DataTable dt = new DataTable(); String conStr = "数据源=172.16.6.173;初始目录=servion_hari;用户=sa;密码=Servion@123"; SqlConnection con = new SqlConnection(conStr); _strsql = "select * from tblproduct where id=@id and Name=@Name and Description=@Description";
【解决方案3】:

你不见了

cmd.Parameters.Add(id);

在执行命令之前添加您的@id 参数。

【讨论】:

    【解决方案4】:

    您得到的错误是因为您只是声明了 SqlParameter 而没有将其分配给您的 SqlCommand。

    SqlParameter id = new SqlParameter("@id", SqlDbType.Int.ToString());
    
    id.Value = GridView1.ToString();
    

    改成

    cmd.Parameters.Add(new SqlParameter("@id", GridView1.ToString()));
    

    问题:您的 GridView1.ToString() 是什么?你打算分配给什么?

    【讨论】:

    • 实际上我有一个包含一些数据的后端数据库,我需要检索数据并存储在缓存中,并为接下来 15 分钟访问该站点的人检索数据。所以我正在用 gridview 计划这个,有什么建议
    • gridview 用于存储一组数据,你需要将你的 id、name、Description 保存在某个地方,你不能只使用 GridView1.ToString()。因此,即使您更正了 @id 分配,您也会在名称和描述上遇到另一个错误
    【解决方案5】:
     protected void Button3_Click(object sender, EventArgs e)
    {
        viewrecord s = new viewrecord();
        s.std_id = Convert.ToInt32(TextBox2.Text);
        SqlConnection conn2 = new SqlConnection(connstring);
        try
        {
            SqlDataAdapter da = new SqlDataAdapter("viewrecord", conn2);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("@std_id", s.std_id);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataSourceID = "";
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Response.Redirect("ErrorMsgBoxBack.aspx");
        }
        finally
        {
            conn2.Close();
        }
    

    }

    【讨论】:

    • 这如何解决问题中的问题?虽然它可能是看起来适用于类似内容的代码,但它似乎是从与该问题不直接相关的其他项目复制和粘贴的。如果您编辑代码使其与问题中的代码直接相关,那就更好了。
    • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请编辑您的答案以添加解释,并说明适用的限制和假设。
    猜你喜欢
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2022-01-07
    • 2020-05-21
    • 1970-01-01
    • 2016-02-20
    • 2019-05-15
    相关资源
    最近更新 更多