我正在使用下拉列表和复选框制作自定义网格视图。复选框表示要调用的数据库中的列。我的codebehind文件是创建一个自定义的SQL查询,我回调给asp:DqlDataSource ... SelectCommand =“”
截至目前,我正在尝试将一条消息存储到(查询)变量中,这是我目前卡住的地方......
<asp:DropDownList ID="DDL" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Fullname" DataValueField="Employee_ID" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="@Query">
</asp:SqlDataSource>
<asp:CheckBox ID="Address" Text="Address" runat="server" />
<asp:CheckBox ID="Phone" Text="Phone" runat="server" />
<asp:CheckBox ID="Email" Text="Email" runat="server" />
<asp:Button ID="ButtonID" onclick="Create" runat="server" Text="Submit" />
<br />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label> -
<asp:Label ID="Label2" runat="server" Text=""></asp:Label> -
<asp:Label ID="Label3" runat="server" Text=""></asp:Label> -
<asp:Label ID="Label4" runat="server" Text=""></asp:Label> -
<br />
<asp:Label ID="Label5" runat="server" Text=""></asp:Label>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
======================= CodeBehind ======================= ====
protected void Create(object sender, System.EventArgs e)
{
string a = "";
string b = "";
string c = "";
string d = "";
if (DDL.SelectedValue == "Select")
{
return;
}
else
{
a = DDL.SelectedValue;
}
if (Address.Checked == true)
{
b = "Address,";
}
if (Phone.Checked == true)
{
c = "Phone,";
}
if (Email.Checked == true)
{
d = "Email,";
}
Label1.Text = a;
Label2.Text = b;
Label3.Text = c;
Label4.Text = d;
string Query = ("SELECT" b c d "FROM Employee WHERE Employee_ID =" a);
}
答案 0 :(得分:0)
字符串连接是你要找的吗?
尝试使用:
string Query = String.Format("SELECT {0} {1} {2} FROM Employee WHERE Employee_ID ={3}",b,c,d,a);
答案 1 :(得分:0)
你到底遇到了什么问题?
我猜你在运行该查询时遇到问题,因为它在FROM
之前会有一个逗号。保存要搜索的字段的更好方法可能是将值推送到单个数组中,然后循环遍历数组的内容并用逗号分隔。这样,您的字段列表可以增长和缩小到任何大小,而不必担心空值。您也可以考虑将“a”值包装在单引号中(假设您正在读取字符串...)
旧代码:
string Query = ("SELECT" b c d "FROM Employee WHERE Employee_ID =" a);
结果: * SELECT地址,电话,电子邮件,FROM员工WHERE Employee_ID = Bob Jones *
新代码:
string Query = "SELECT " + String.Join(", ", arrFields) + " FROM Employee WHERE Employee_ID = '" + a + "'"
结果: * SELECT地址,电话,电子邮件来自员工WHERE Employee_ID ='Bob Jones'*