简体   繁体   English

动态将SQL语句用于SQLDataSource

[英]Dynamically using SQL statements for SQLDataSource

I may or may not have a pretty simple question here for you guys. 我可能对您有一个非常简单的问题。 Basing my if-else statements off of the existence of a string, I am trying to either call a SELECT statement with a parameter or not and pass them both to the same resulting GridView. 基于字符串是否存在我的if-else语句,我试图用一个参数调用SELECT语句或不使用一个参数,并将它们都传递给相同的结果GridView。

Here is what I am trying to do: 这是我正在尝试做的事情:

 string query;
 if(BadgeNumLabel.Text != "")
 {
     query = "SELECT * FROM AUDITS";
 else
 {
     query = "SELECT * FROM AUDITS WHERE BADGENUM = :BadgeNumLabel";
     GridDataSource.SelectParameters.Add(new Parameter("BadgeNumLabel",TypeCode.String, BadgeNumLabel.Text));
 }

 GridDataSource.SelectCommand = query;
 GridView1.DataBind();

My .aspx code looks like this: 我的.aspx代码如下所示:

 <asp:SqlDataSource ID="GridDataSource" runat="server"
            ConnectionString="<%$ ConnectionStrings:OracleConnectionString %>" 
            ProviderName="<%$ ConnectionStrings:OracleConnectionString.ProviderName %>"  
            onselecting="GridDataSource_Selecting">


        </asp:SqlDataSource>  

Is there something I am missing? 我有什么想念的吗? I am so stumped. 我好难过 I think it has to do something with passing the parameter BadgeNumLabel around but I'm not sure. 我认为它必须通过传递参数BadgeNumLabel来做些事情,但是我不确定。

Any help is greatly appreciated! 任何帮助是极大的赞赏! Thanks! 谢谢!

You need to assign the result of the GridDataSource to the GridView1.DataSource 您需要将GridDataSource的结果分配给GridView1.DataSource

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand GridDataSource= new SqlCommand();

GridDataSource.CommandText = "SELECT * FROM AUDITS";
GridDataSource.CommandType = CommandType.Text;
GridDataSource.Connection = sqlConnection1;

sqlConnection1.Open();

GridView1.DataSource = GridDataSource.ExecuteReader();
GridView1.DataBind();

EDIT 编辑

It's not literally ExecuteCommand, what I try to say is that you need to assign the result of the SqlCommand to the GridView 它不是ExecuteCommand,我想说的是您需要将SqlCommand的结果分配给GridView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM