我已經檢查了本網站上的大部分論壇,但是沒有得到我的解決方案。 我的問題是從vb.net插入數據到MS Access,但我無法做到。 它沒有顯示任何錯誤,但也沒有在我的表中插入值。 我使用非常簡單的代碼:使用vb.net插入和更新MS Access中的值
Imports System.Data.OleDb
Public Class Add_LEads
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Private Sub Add_LEads_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\IndGlobalDB.accdb;Persist Security Info=True;Jet OLEDB:Database Password=admin")
lblDate.Text = Format(Date.Now, "yyyy/MM/dd")
conn.Open()
Dim sql As String
Dim a As Integer
sql = "select S_No from Leadss"
cmd = New OleDbCommand(sql, conn)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader
While dr.Read
a = dr(0)
End While
lblNo.Text = a + 1
conn.Close()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO Leadss(S_No,Contact_Person,Mobile_No,Email_Id,Description,First_Follow_Up,Remarks,L_Date,Alternate_no)VALUES('" & lblNo.Text & "','" & txtName.Text & "','" & txtMobile.Text & "','" & txtEmail.Text & "','" & txtWebDescr.Text & "','" & txtFollowUp.Text & "','" & txtRemarks.Text & "','" & lblDate.Text & "','" & txtAlternate.Text & "')"
cmd.ExecuteNonQuery()
conn.Close()
MsgBox("Saved!!!", vbOK)
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
Welcome.Show()
End Sub
End Class
爲例你問[SQL注入攻擊(http://en.wikipedia.org/wiki/SQL_injection)。 – Eonasdan
你從「cmd.CommandText =」插入的結果是什麼?INSERT INTO Leadss(S_No,Contact_Person,Mobile_No,Email_Id,Description,First_Follow_Up,Remarks,L_Date,Alternate_no)VALUES(''&lblNo.Text&「','」&txtName .Text&「','」&txtMobile.Text&「','」&txtEmail.Text&「','」&txtWebDescr.Text&「','」&txtFollowUp.Text&「','」&txtRemarks .Text&「','」&lblDate.Text&「','」&txtAlternate.Text&「')」「 – andy
使用try/catch語句塊查看可能出錯的內容。也出於安全原因,請使用SqlParameters而不是直接將TextBox值放入您的SQL語句中。 – Abbas