请教vb.net向access插入数据问题
我建两个表单,在form1 从access里读取数据并显示,在form2里向access里写数据,现在出现一个问题,在form2里向access插入一条记录的时候,没有报错,但是在form1里有事能读出放写进去的数据,有时候就不能读出来;再打开access文件,里面什么数据都没有,请问各位大神,这是什么原因呀?我插入记录的代码如下:Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bwl.accdb"
Dim myconn As New OleDbConnection(connectionString)
myconn.Open()
Dim mySQL As String = "INSERT INTO bwl (riqi,dbsj) VALUES ('" & DateTimePicker1.Value & "','" & TextBox1.Text & "')"
Dim objCmd As New OleDbCommand(mySQL, myconn)
objCmd.ExecuteNonQuery()
myconn.Close()
Me.Close() --------------------编程问答-------------------- 把 Dim mySQL As String = "INSERT INTO bwl (riqi,dbsj) VALUES ('" & DateTimePicker1.Value & "','" & TextBox1.Text & "')"
改成Dim mySQL As String = "INSERT INTO bwl (riqi,dbsj) VALUES ('" + DateTimePicker1.Value + "','" + TextBox1.Text + "')"
试试... --------------------编程问答-------------------- TextBox1.Text带引号吗?带的话,要写成
TextBox1.Text.Replace("'","''")
另外最好参数化sql
--------------------编程问答-------------------- Dim mySQL As String = "INSERT INTO bwl (riqi,dbsj) VALUES (@riqi,@dbsj)"
Dim objCmd As New OleDbCommand(mySQL, myconn)
objCmd.Parameters.AddWithValue("@riqi",DateTimePicker1.Value)
objCmd.Parameters.AddWithValue("@dbsj",TextBox1.Text)
objCmd.ExecuteNonQuery() --------------------编程问答--------------------
有可能有特殊字符 --------------------编程问答-------------------- Dim mySQL As String = "INSERT INTO bwl (riqi,dbsj) VALUES ('" & DateTimePicker1.Value & "','" & TextBox1.Text & "')"
改为:
Dim mySQL As String = "INSERT INTO bwl (riqi,dbsj) VALUES ('" & DateTimePicker1.Value & "','" & trim(TextBox1.Text) & "')"
补充:.NET技术 , VB.NET