我在Visual Basic 2008中创建了Form1,在Form1上我放置了两个TextBox,两个DateTimePicker和一个Button。在M. S. Access中,我创建了一个包含两个表tableA和tableB的数据库。在这里,我只想从tableA中检索选定的记录,并插入到tableB中。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TAMBE-DATABASE\SANJAY_PUNE.mdb")
Connection.Open()
Command = New OleDbCommand(" insert into tableB select * FROM tableA ", Connection)
Command.ExecuteNonQuery()
Command.Dispose()
Connection.Close()
MsgBox("Records Added Successfully")
End Sub
当我尝试执行它时,所有行都已添加到表B中。我不想添加所有记录。我想只添加选定的行。当我将选定的声明更改为
时Command = New OleDbCommand(" insert into tableB select * FROM tableA where DATE_OF_SELL BETWEEN '" & TextBox1.Text "' AND '" & TextBox2.Text "'", Connection)
在文本框中,我选择了一个日期From和To,因为我想从DATE_OF_SELL
字段中检索所选日期。当我尝试执行它时出错。
答案 0 :(得分:0)
使用参数,或使用> = =<运算符。
参数更清晰,更具可读性:
Command = New OleDbCommand(" insert into tableB select * FROM tableA where DATE_OF_SELL BETWEEN @FROM AND @END", Connection)
Command.Parameters.AddWithValue("@FROM", TextBox1.Text)
Command.Parameters.AddWithValue("@END", TextBox2.Text)
但是你为什么使用TextBox而不是DateTimePicker呢?它要求你执行验证和转换(以不同格式的情况)由@Vignesh Kumar撰写
答案 1 :(得分:0)
尝试像这样使用
Command = New OleDbCommand(" insert into tableB select * FROM tableA where DATE_OF_SELL BETWEEN Convert(DateTime,'" & TextBox1.Text "',105) AND Convert(DateTime,'" & TextBox2.Text "',105)", Connection)