如何在WHERE中使用2个文本框?

时间:2014-03-11 10:11:36

标签: vb.net

如何在WHERE OrderDate中使用2个文本框(txtFrom和txtTo)?对不起,我是新人,我只是不知道该放什么。感谢

 Dim da As New OleDb.OleDbDataAdapter("SELECT CustomerName, ProductName, Quantity, Amount, OrderDate FROM Orders WHERE Orderdate", con)

1 个答案:

答案 0 :(得分:3)

试试这个

Dim da As New OleDb.OleDbDataAdapter("SELECT CustomerName, ProductName, Quantity, Amount, OrderDate FROM Orders WHERE Orderdate Between '" + txtFrom.Text + "' AND '" + txtTo.Text + "'", con)

但这不是推荐。尝试使用参数化查询,就像这样

String sSQL = "SELECT CustomerName, ProductName, Quantity, Amount, OrderDate FROM Orders WHERE Orderdate Between @Start And @End";        
OleDbConnection conn = new OleDbConnection("ConnectionString");    
OleDbCommand cmd = new OleDbCommand();         
cmd.Connection = conn;    
cmd.CommandType = CommandType.Text;    
cmd.CommandText = sb.ToString();         
cmd.Parameters.AddWithValue("@Start ", txtFrom.Text);    
cmd.Parameters.AddWithValue("@End", txtTo.Text);