我收到此错误,我无法弄清楚如何解决此问题,有人帮助这里是我的代码。另外,我正在使用Microsoft Access数据库(.accdb)和我的C:\ Users ***** **\桌面 我收到错误[IM002] [Microsoft] [ODBC驱动程序管理器]未找到数据源名称,并且未指定默认驱动程序
Dim strSql As String =" INSERT INTO *来自Table1"
Dim strPath As String =" Provider = Microsoft.ACE.OLEDB.12.0;" &安培; "数据源= C:\用户******** \桌面\ VBgameDatabase.accdb"
Dim odaTravel As New OleDb.OleDbDataAdapter(strSql, strPath)
Dim datCost As New DataTable
Dim Connection2 As New System.Data.Odbc.OdbcConnection(strPath)
Connection2.Open()
Dim cmd As OdbcCommand = New OdbcCommand("INSERT INTO accdb (Slot, Health, Gold, Level, XP, Strength, Vitality, Agility) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Connection2)
cmd.Parameters.Add("@Health", OdbcType.VarChar, 255).Value = Battle.lblPlayerHealth.Text
cmd.ExecuteNonQuery()
cmd.Parameters.Add("@Gold", OdbcType.VarChar).Value = Inventory.lblMoney.Text
cmd.ExecuteNonQuery()
cmd.Parameters.Add("@Level", OdbcType.VarChar, 255).Value = Inventory.lblLevel.Text
cmd.ExecuteNonQuery()
cmd.Parameters.Add("@XP", OdbcType.VarChar, 255).Value = Inventory.lblXP.Text
cmd.ExecuteNonQuery()
cmd.Parameters.Add("@Strength", OdbcType.VarChar, 255).Value = Stats.lblStrength.Text
cmd.ExecuteNonQuery()
cmd.Parameters.Add("@Vitality", OdbcType.VarChar, 255).Value = Stats.lblVitality.Text
cmd.ExecuteNonQuery()
cmd.Parameters.Add("@Agility", OdbcType.VarChar, 255).Value = Stats.lblAgility.Text
cmd.ExecuteNonQuery()
答案 0 :(得分:0)
首先,通过ODBC访问的连接字符串如下
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _
"Dbq=C:\Users********\Desktop\VBgameDatabase.accdb;Uid=Admin;Pwd="
最后,您添加了7个参数,但表中列出的字段为8,因此您需要另一个参数。当然,在添加所有参数后,应该只调用一次ExecuteNonQuery。
Dim Connection2 As New System.Data.Odbc.OdbcConnection(strPath)
Connection2.Open()
Dim cmd As OdbcCommand = New OdbcCommand("INSERT INTO accdb " & _
"(Slot, Health, Gold, Level, XP, Strength, Vitality, Agility) " & _
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Connection2)
' MISSING PARAMETER FOR SLOT FIELD
' cmd.Parameters.Add("@Slot", ......)
cmd.Parameters.Add("@Health", OdbcType.VarChar, 255).Value = Battle.lblPlayerHealth.Text
cmd.Parameters.Add("@Gold", OdbcType.VarChar).Value = Inventory.lblMoney.Text
cmd.Parameters.Add("@Level", OdbcType.VarChar, 255).Value = Inventory.lblLevel.Text
cmd.Parameters.Add("@XP", OdbcType.VarChar, 255).Value = Inventory.lblXP.Text
cmd.Parameters.Add("@Strength", OdbcType.VarChar, 255).Value = Stats.lblStrength.Text
cmd.Parameters.Add("@Vitality", OdbcType.VarChar, 255).Value = Stats.lblVitality.Text
cmd.Parameters.Add("@Agility", OdbcType.VarChar, 255).Value = Stats.lblAgility.Text
cmd.ExecuteNonQuery()
顺便问一下,你确定你的桌子叫做ACCDB吗?这看起来有点奇怪......