2015-10-15 113 views
1

我需要將MySQL數據庫綁定到下拉列表的幫助。將mysql數據庫綁定到下拉列表vb.net

這裏是如何我通常連接到數據庫

Dim connectionstring As String = ConfigurationManager.ConnectionStrings("DBstring").ConnectionString 
Dim connectme As OdbcConnection = New OdbcConnection(connectionstring) 
Dim ODBCdataset As DataSet = New DataSet() 
Dim sqlquery As String = "SELECT * FROM treconcalculation WHERE fid = " & Request.QueryString("id") 
connectme.Open() 
Dim ODBCdataadapter As OdbcDataAdapter = New OdbcDataAdapter(sqlquery, connectme) 
ODBCdataadapter.Fill(ODBCdataset, "treconcalculation") 
connectme.Close() 

      make1 = ODBCdataset.Tables("treconcalculation").Rows(0).Item(1) 
      model1 = ODBCdataset.Tables("treconcalculation").Rows(0).Item(2) 
      cc1 = ODBCdataset.Tables("treconcalculation").Rows(0).Item(3) 
      below2 = ODBCdataset.Tables("treconcalculation").Rows(0).Item(4) 
      below3 = ODBCdataset.Tables("treconcalculation").Rows(0).Item(5) 
      below4 = ODBCdataset.Tables("treconcalculation").Rows(0).Item(6) 
      below5 = ODBCdataset.Tables("treconcalculation").Rows(0).Item(7) 
      above5 = ODBCdataset.Tables("treconcalculation").Rows(0).Item(8) 

我怎麼去實現它的下拉列表?我試過

Dim constr As String = ConfigurationManager.ConnectionStrings("DBstring").ConnectionString 
      Using con As New SqlConnection(constr) 
       Using cmd As New SqlCommand("SELECT fid, fmake, fmodel FROM treconcalculation") 
        cmd.CommandType = CommandType.Text 
        cmd.Connection = con 
        Using sda As New SqlDataAdapter(cmd) 
         Dim ds As New DataSet() 
         sda.Fill(ds) 
         vehicle1.DataSource = ds.Tables(0) 
         vehicle1.DataTextField = "fmodel" 
         vehicle1.DataValueField = "fid" 
         vehicle1.DataBind() 
        End Using 
       End Using 
      End Using 
      vehicle1.Items.Insert(0, New ListItem("--Select Customer--", "0")) 

並且出現一個錯誤提示說不支持關鍵字:'driver'。

感謝

+0

哪條線給你那個錯誤? –

+0

我發現這條線出了問題。 connectionString =「DRIVER = {MySQL ODBC 3.51 Driver}是否可以將腳本更改爲ODBC驅動程序? – Jacklck

回答

0

所以,問題不在於下拉結合:它與連接字符串,您正在使用連接到數據庫:我請你檢查ODBC驅動程序3.51是否已經安裝在你的機器並檢查具有所有有效參數的連接字符串。如果你正在代碼中建立連接字符串,我建議你使用如下:

Dim _DbHost = "localhost"; 
Dim _DbName = "yourDBName"; 
Dim _DbPassWord = "DbUser"; 
Dim _DbUser = "*****"; 
Dim ConString = "Driver={MySQL ODBC 3.51 Driver};" + 
       "Server=" + DbHost + ";" + 
       "Database=" + DbName + ";" + 
       "User=" + DbUser + ";" + 
       "Password=" + DbPassWord + ";" + 
       "option=3;"