| 查看: 1059 | 回复: 1 | ||
keke226铁杆木虫 (正式写手)
|
[求助]
关于Visual studio运行VB脚本中,查询ACCESS数据库所用的SQL语言中引入变量的问题
|
|
最近写一个程序,运行环境VS2010,用VB编程,连接ACCESS2010数据库想实现如下功能: 在Web网页中生成一个DownDropList,一个Button和一个ListBox,想通过DownDropList里面选择不同的Item,然后点击Button来实现对ACCESS数据库的查询功能,但是发现在运行程序时,总是提示mycmd.ExecuteReader()缺少值,应该是SELECT语句里的变量出了问题(下面代码中的粗体句),请问如何这个SOL语句中引入变量??? Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click If DropDownList1.SelectedItem.Text = "所有比赛" Then Dim mystr As String, mysql As String Dim myconn As New OleDbConnection() Dim mycmd As New OleDbCommand() mystr = "Provider = Microsoft.ACE.OLEDB.12.0;" & "Data source = " & Server.MapPath("~\\App_data\\game.accdb" ![]() myconn.ConnectionString = mystr myconn.Open() mysql = "SELECT 比赛日期,主队,客队,小组 FROM Game" mycmd.CommandText = mysql mycmd.Connection = myconn Dim myreader As OleDbDataReader = mycmd.ExecuteReader() ListBox1.Items.Add("比赛日期 主队 客队 小组" ![]() ListBox1.Items.Add("========" ![]() While myreader.Read() ListBox1.Items.Add(String.Format("{0} {1} {2} {3}", myreader(0).ToString(), myreader(1).ToString, myreader(2).ToString, myreader(3).ToString)) End While myconn.Close() myreader.Close() Else Dim mystr As String, mysql As String, str As String Dim myconn As New OleDbConnection() Dim mycmd As New OleDbCommand() mystr = "Provider = Microsoft.ACE.OLEDB.12.0;" & "Data source = " & Server.MapPath("~\\App_data\\game.accdb" ![]() myconn.ConnectionString = mystr str = DropDownList1.SelectedItem.Text myconn.Open() mysql = "SELECT 比赛日期,主队,客队,小组 FROM Game where 主队 or 客队=" + DropDownList1.SelectedItem.Text.ToString mycmd.CommandText = mysql mycmd.Connection = myconn Dim myreader As OleDbDataReader = mycmd.ExecuteReader() ListBox1.Items.Add("比赛日期 主队 客队 小组" ![]() ListBox1.Items.Add("========" ![]() While myreader.Read() ListBox1.Items.Add(String.Format("{0} {1} {2} {3}", myreader(0).ToString(), myreader(1).ToString, myreader(2).ToString, myreader(3).ToString)) End While myconn.Close() myreader.Close() End If End Sub |
» 猜你喜欢
291分调剂
已经有9人回复
调剂求收留
已经有34人回复
291 求调剂
已经有38人回复
22408 312求调剂
已经有17人回复
一志愿华中农业071010,320求调剂
已经有6人回复
290调剂生物0860
已经有41人回复
291求调剂
已经有3人回复
211本科材料化工求调剂
已经有23人回复
山东省基金2026
已经有9人回复
药学求调剂
已经有13人回复

2楼2016-12-27 19:11:50















回复此楼