我有一个带有两个文本框的计算器,用户在每个文本框中输入一个数字。然后,他们单击加号,减号,除法或乘法按钮,它会对数字执行此功能。这些数字将保存到名为listbox1的列表框中。当用户单击以显示结果时,将使用所有已保存的值填充列表框,并且应用程序是SUPPOSED以将列表框项目保存到访问数据库。它不起作用。下面是我的代码,其中numFirst是数据库表中类别的名称:
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles
btnDisplay.Click
ListBox1.Items.Clear()
For arrayindex As Integer = 0 To intarrayCount - 1
ListBox1.Items.Add(Input(arrayindex))
ListBox1.Text = Convert.ToString(ListBox1.Items.Item(arrayindex))
Next arrayindex
Dim query As String = "SELECT * FROM wk6"
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\helse_000\Desktop\APU\VB Advanced\week4\ENTD461_wk4_Andrew_Helsel\ENTD461_wk2_Andrew_Helsel\calculator.mdb"
Dim command As OleDbCommand = New OleDbCommand
Dim var1 As String = Convert.ToString(ListBox1.Items.Item(0))
command.CommandText = "INSERT into wk6 (numFirst) VALUES (" + var1 + ")"
答案 0 :(得分:0)
想出来,删除了select all查询字符串,并在修改结果后将我的文本框字段变为参数,以适应表格所需的格式。
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim firstString As String = Convert.ToString(ListBox1.Items.Item(i))
Dim leftPart As String = firstString.Split(Convert.ToChar
(strButtonSelected))(0)
Dim rightPart As String = firstString.Split(Convert.ToChar("="))(1)
Dim a As Integer = firstString.IndexOf(strButtonSelected)
Dim b As Integer = firstString.IndexOf("=")
Dim secNum = firstString.Substring(a + 1, b - 4)
secNum = secNum.Trim
Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\Users\helse_000\Desktop\APU\VB Advanced\week4\ENTD461_wk4_Andrew_Helsel\ENTD461_wk2_Andrew_Helsel\calculator.mdb")
Dim command As OleDbCommand = New OleDbCommand
Dim cmdstring As String = "INSERT INTO wk6 (numFirst, numSecond, Operator, equals, Answer)" + " VALUES (@firstName,@lastName,@Operator,@equals,@answer)"
command = New OleDbCommand(cmdstring, conn)
command.Parameters.AddWithValue("@firstName", leftPart)
command.Parameters.AddWithValue("@lastName", secNum)
command.Parameters.AddWithValue("@Operator", strButtonSelected)
command.Parameters.AddWithValue("@equals", "=")
command.Parameters.AddWithValue("@answer", rightPart)
conn.Open()
command.ExecuteNonQuery()
conn.Close()