我尝试在字段年份和实际数据下插入10条记录,但只有text1中的第一个文字进入内部 如何将两个或多个文本框中的值保存到同一个字段中,例如年
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "db7exmpl.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
'str = " INSERT INTO tb (Years) VALUES('" & TextBox1.Text & "," & TextBox3.Text & "," & TextBox5.Text & "');"
str = "insert into tb ([Years], [Actual_data]) values (?, ?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox1.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox3.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox5.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox7.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox9.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox11.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox13.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox15.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox17.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Years", CType(TextBox19.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox2.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox4.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox6.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox8.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox10.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox12.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox14.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox16.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox18.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Actual_data", CType(TextBox20.Text, String)))
Try
cmd.ExecuteNonQuery()
'myConnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
答案 0 :(得分:0)
您需要为每对执行单独的查询:
Using cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=db7exmpl.accdb")
cn.Open()
For x = 1 To 19 Step 2
Using cmd = New OleDbCommand("INSERT INTO TB ([Years], [Actual_Data]) VALUES (?,?)", cn)
cmd.Parameters.AddWithValue("Years", CType(Controls("TextBox" & x), TextBox).Text)
cmd.Parameters.AddWithValue("Actual_Data", CType(Controls("TextBox" & (x + 1)), TextBox).Text)
cmd.ExecuteNonQuery()
End Using
Next
End Using