我正在尝试创建一个搜索,其中SQL查询依赖于单选按钮和文本框。
SELECT * FROM [Sales] WHERE (text in radio button) = (text in textbox)
我该如何实现?
以下是我的完整代码。
Protected Sub btnSearch_Click(sender As Object, e As ImageClickEventArgs) Handles btnSearch.Click
If radClient.Checked = True Then
Dim connection As New OleDb.OleDbConnection
Dim provider As String
Dim source As String
provider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
source = "Data Source=|DataDirectory|DeltaOneMDB.mdb"
connection.ConnectionString = provider & source
GridView1.Visible = False
connection.Open()
SqlDataSource3.SelectCommand = "SELECT * FROM Sales WHERE ClientID = '" & txtSearch.Text & "'"
connection.Close()
ElseIf radItem.Checked = True Then
Dim connection As New OleDb.OleDbConnection
Dim provider As String
Dim source As String
provider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
source = "Data Source=|DataDirectory|DeltaOneMDB.mdb"
connection.ConnectionString = provider & source
GridView1.Visible = False
connection.Open()
SqlDataSource3.SelectCommand = "SELECT * FROM Sales WHERE ItemID = '" & txtSearch.Text & "'"
connection.Close()
ElseIf radUser.Checked = True Then
Dim connection As New OleDb.OleDbConnection
Dim provider As String
Dim source As String
provider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
source = "Data Source=|DataDirectory|DeltaOneMDB.mdb"
connection.ConnectionString = provider & source
GridView1.Visible = False
connection.Open()
SqlDataSource3.SelectCommand = "SELECT * FROM Sales WHERE UserID = '" & txtSearch.Text & "'"
connection.Close()
ElseIf radUser.Checked = False And radClient.Checked = False And radItem.Checked = False Then
ErrorLabel.Text = "Error: You have not chosen a search criteria."
End If
End Sub
表格,文本框和单选按钮都在同一表格中。我正在使用Microsoft Visual Studio 2012 for Web,并尝试更改窗体上GridView对象的数据源。但是,我一直收到错误:
尚未初始化ConnectionString属性。
抱歉不提前提交我的代码。
谢谢!
答案 0 :(得分:2)
查看你的代码,我看到很多不必要的行。我想举例说明我认为您可以采取哪些措施来简化代码。 首先,删除SqlDataSource不需要的代码(我想你已经打开了与数据库的连接,但是这可以在点击输入时进行一次。 其次,在每个交换机中准备要执行的命令以及为该命令传递的参数(名称和值) 最后更新你的SqlDataSource并重新绑定所有内容
Protected Sub btnSearch_Click(sender As Object, e As ImageClickEventArgs) Handles btnSearch.Click
Dim sqlQuery As String
Dim prm as String
Dim prmValue as String
prmValue = txtSearch.Text
If radClient.Checked = True Then
sqlQuery = "SELECT * FROM Sales WHERE ClientID = @clientID"
prm = "@clientID"
ElseIf radItem.Checked = True Then
sqlQuery = "SELECT * FROM Sales WHERE ItemID = @itemID"
prm = "@itemID"
ElseIf radUser.Checked = True Then
sqlQuery = "SELECT * FROM Sales WHERE UserID = @userID"
prm = "@userID"
ElseIf radUser.Checked = False And radClient.Checked = False And radItem.Checked = False Then
ErrorLabel.Text = "Error: You have not chosen a search criteria."
return
End If
SqlDataSource3.SelectCommand = sqlQuery
SqlDataSource3.SelectParameters.Add(prm, prmValue)
SqlDataSource3.DataBind()
End Sub
答案 1 :(得分:1)
使用SQLDatasource时,您不必指定Connections,它管理自己的连接。
您需要做的是:
SqlDataSource3.DataBind()
此外,更好的方法是使用允许空值的存储过程。 更适合SQL注入(数据库的威胁)
SELECT * FROM Sales WHERE (@ClientID IS NULL OR ClientID = @ClientID)
AND (@ItemID IS NULL OR ItemID = @ItemID )
AND (@UserIDIS NULL OR UserID= @UserID)
答案 2 :(得分:-1)
互联网上无处不在的解决方案。这可能是世界上最简单的。
PrintWriter out=response.getWriter();
out.print("<html>");
out.print("<body>");
out.print("<form>");
out.print("<textarea rows='5' columns='500' name='t1'></textarea>");
//out.print("<input type='text' name='t2'>");
//out.print("c<input type='checkbox' value='c' name='t1'>");
//out.print("d<input type='checkbox' value='d' name='t1'>");
out.print("<input type='submit' value='execute' name='b1'>");
// out.print("<input type='submit' value='show' name='b1'>");
//out.print("<input type='submit' value='query' name='b1'>");
String a,c;
a=request.getParameter("t1");
//b=request.getParameter("t2");
c=request.getParameter("b1");
if(c!=null)
{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/saransh","root","");
Statement st=con.createStatement();
if(a.startsWith("delete"))
{
out.print("<br>");
out.print("delete query execution...");
//st.executeUpdate("insert into student2(name,address) values('"+a+"',"+b+"')") ;
st.executeUpdate(a);
}
else if(a.startsWith("select"))
{ out.print("<br>");
out.print("select query execution...");
ResultSet rs=st.executeQuery(a);
ResultSetMetaData m1=rs.getMetaData();
out.print("<br>");
for(int i=1;i<=m1.getColumnCount();i++)
{String x1=m1.getColumnLabel(i);
out.print(" "+x1);}
out.print("<br>");//name of label
while(rs.next())
{ String s0=rs.getString("Stu_roll");
String s1=rs.getString("name");
String s2=rs.getString("address");
out.print(s0+" "+s1+" "+s2);
out.print("<br>");
}}
else if(a.startsWith("insert"))
{ out.print("<br>");
out.print("insert query execution...");
st.executeUpdate(a);
}
else if(a.startsWith("drop"))
{ out.print("<br>");
out.print("drop query execution...");
st.executeUpdate(a);
}
} catch (Exception e) {
out.print(e);
// TODO: handle exception
}
}
out.print("</form>");
out.print("</body>");
out.print("</html>");
删除注释行。唯一的问题是查询区分大小写。您可以弄清楚要使用什么字符串操作来“忽略大小写”。