使用我正在运行的当前代码不会发生任何错误,但是没有数据添加到Microsoft Access DB。如果代码中包含2个或更少的ComboBox,它只会将数据上传到DB。这让我非常困惑。我已经尝试过ComboBox1.SelectedItem.ToString,ComboBox1.SelectedItem.Text和ComboBox1.Text以及所有这些问题。有什么想法吗?
Imports System.Data.OleDb
Public DefineDataFile As String
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
dataFile = "XXXXXXXXXXXXX.mdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "INSERT INTO [Panels/Equipment] ([Panel/Equipment Name], [Machine Name], [Panel/Equipment #], [Type], [Functional Location], [Facility], [Category], [Lifetime Cost Savings], [Hours Per Day], [Days Per Week], [Weeks Per Year], [Description], [Environmental Condition], [Environmental Option], [Environmental Finding], [Heating/Cooling Condition], [Heating/Cooling Option], [Heating/Cooling Finding], [Wiring Condition], [Wiring Option], [Wiring Finding], [Machines ID])"
str += "SELECT ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,[Machines].[Machines ID]"
str += "FROM [Machines] WHERE [Machines].[Machine Name] = ? "
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("Panel/Equipment Name", CType(TextBox_Name.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Machine Name", CType(TextBox_Machine.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Panel/Equipment #", CType(TextBox_PENum.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Type", CType(ComboBox_Type.SelectedItem.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Functional Location", CType(TextBox_FunctionalLocation.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Facility", CType(TextBox_Facility.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Category", CType(ComboBox_Category.SelectedItem.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Lifetime Cost Savings", CType(TextBox_LifetimeCostSavings.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Hours Per Day", CType(TextBox_HoursPerDay.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Days Per Week", CType(TextBox_DaysPerWeek.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Weeks Per Year", CType(TextBox_WeeksPerYear.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Priority", CType(ComboBox_Priority.SelectedItem.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Description", CType(TextBox_Description.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Environmental Condition", CType(ComboBox_EC.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Environmental Option", CType(ComboBox_EO.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Environmental Finding", CType(ComboBox_EF.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Heating/Cooling Condition", CType(ComboBox_HCC.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Heating/Cooling Option", CType(ComboBox_HCO.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Heating/Cooling Finding", CType(ComboBox_HCF.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Wiring Condition", CType(ComboBox_WC.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Wiring Option", CType(ComboBox_WO.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Wiring Finding", CType(ComboBox_WF.SelectedItem.Text, String)))
* cmd.Parameters.Add(New OleDbParameter("Machine ID", CType(ComboBox_Machine.SelectedItem.ToString, String)))
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
MsgBox("Panel/Equipment Successfully Added")
Catch ex As Exception
MsgBox(ex.Message)
End Try
myConnection.Close()
答案 0 :(得分:1)
为什么要将文本转换为字符串?在CType(TextBox_Name.Text, String)
中,表达式TextBox_Name.Text
已经是一个字符串。只需写下
cmd.Parameters.Add(New OleDbParameter("Panel/Equipment Name", TextBox_Name.Text))
对于其他参数,您必须提供与表列中的类型匹配的类型的值。例如。如果Access中的机器ID是数字长(32位),则写入
cmd.Parameters.Add(New OleDbParameter("Machine ID", _
CType(ComboBox_Machine.SelectedItem, Integer))) 'Integer in VB is 32-bit
你还有21 ?
,但是插入了23个参数。 Priority
参数似乎错了。由于您直接在INSERT中插入Machine ID
,因此不应为其添加参数,而应在where子句中为Machine Name
添加一个参数。
SelectedItem
的{{1}}属性返回ComboBox
。对象不包含属性Object
。组合框包含哪些对象?如果您添加了字符串,那么Text
已经是一个字符串。 E.g。
SelectedItem
另外,我建议您在VB中使用项目选项cmd.Parameters.Add(New OleDbParameter("Type", ComboBox_Type.SelectedItem))
,因为这会显示未显示的编码错误。