【问题标题】:Modify SqlDataSource.SelectCommand using Stored Procedure with parameters使用带参数的存储过程修改 SqlDataSource.SelectCommand
【发布时间】:2015-01-02 16:23:12
【问题描述】:

我正在尝试在我的应用程序中创建一个搜索框,为此我需要修改 SqlDataSource.SelectCommand。我将不胜感激!

为了测试我是这样做的,它可以工作,但它很容易被 sql 注入攻击

   SqlDataSource1.SelectCommand = "sp_offer_search '" + txtSearch.Text + "', " +   Session["customerId"] + " , '" + "Pending"+ "'";
   GridView1.DataBind();

这是我迄今为止尝试过的,但它不起作用:

 if (txtSearch.Text != "")
        {                
         //open connection
          oCn.Open();
          SqlCommand com = new SqlCommand(query, oCn);
          com.CommandType = CommandType.StoredProcedure;

          com.Parameters.AddWithValue("@Variable", txtSearch.Text);  
          com.Parameters.AddWithValue("@CustomerId",Session["customerId"]);   
          com.Parameters.AddWithValue("@Status", txtStatus.Text);   


            DataTable dt = new DataTable();
            dt.Load(com.ExecuteReader());

            SqlDataSource1.SelectCommand = dt.ToString();
            GridView1.DataBind();
       }

【问题讨论】:

  • 它现在究竟是如何工作的?您是否收到错误、不正确的退货或没有退货?还有什么?
  • 它不返回任何东西,就像它没有找到匹配一样
  • 您应该查看Can we stop using AddWithValue() already? 并停止使用.AddWithValue() - 它可能会导致意想不到和令人惊讶的结果...
  • Marc_s 谢谢你的举手。我不知道。

标签: c# sql sql-server sqldatasource


【解决方案1】:

如果 GridView 数据源设置为 SqlDataSource1,则不需要 DataTable。并且 DataTable.ToString() 不是 selectCommand。试试:

 if (txtSearch.Text != "")
    {                        
      SqlCommand com = new SqlCommand(query, oCn);
      com.CommandType = CommandType.StoredProcedure;

      com.Parameters.AddWithValue("@Variable", txtSearch.Text);  
      com.Parameters.AddWithValue("@CustomerId",Session["customerId"]);   
      com.Parameters.AddWithValue("@Status", txtStatus.Text);   

      SqlDataSource1.SelectCommand = com;
      GridView1.DataBind();
   }

【讨论】:

  • 嗨 Crowcoder,“com”正在返回 System.Data.SqlClient.SqlCommand 并导致错误:此版本的 SQL Server 不支持“System.Data.SqlClient.SqlCommand”有什么想法吗?
  • 你的数据库是什么?
  • 我们正在使用 SQL Azure
  • 我只对 SQL Azure 使用了 EntityFramework。我发现这个 (axian.com/2014/12/01/adjustments-for-sql-azure-no-ole-db) 暗示 ADO.Net OleDB 提供程序可能工作。但是,如果您要切换提供者,我建议您使用 EntityFramework。
【解决方案2】:

解决了!这是我尝试过的,它有效。我希望这对某人有所帮助。

  if (txtSearch.Text != "")
        {
            try
            {
                //  open connection
                oCn.Open();

                SqlDataAdapter da = new SqlDataAdapter("sp_offer_search", oCn);

                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add("@Variable", SqlDbType.VarChar).Value = txtSearch.Text;
                da.SelectCommand.Parameters.Add("@CustomerId", SqlDbType.Int).Value = Session["customerId"];
                da.SelectCommand.Parameters.Add("@Status", SqlDbType.VarChar).Value = "Pending";

                DataTable dt = new DataTable();
                da.Fill(dt);
                GridView1.DataSourceID = String.Empty;
                GridView1.DataSource = dt;
                GridView1.DataBind();

            }
            catch(Exception ex)
            {
                Response.Write(ex.ToString());
            }
            finally
            {
                oCn.Close();
            }

        }
        else
        {
            GridView1.DataSourceID = "SqlDataSource1";
            SqlDataSource1.SelectCommand = SqlDataSource1.SelectCommand;
            GridView1.DataBind();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多