2017-04-07 85 views
0

如何將數據插入MS Access表中?當我嘗試時,我收到一個錯誤。將數據插入MS Access表時出錯

代碼:

If TextBox1.Text = Nothing And TextBox2.Text = Nothing Then 
    MsgBox("No Username and Password inserted") 
    TextBox1.Focus() 

Else 

    If Not con.State = ConnectionState.Open Then 
     'open connection if it is not yet open 
    End If 

    cmd.Connection = con 
    'add data to table 
    cmd.CommandText = "insert into loginTable(username, password, typeofuser) values ('" & Me.TextBox1.Text & "', '" & Me.TextBox2.Text & "', '" & Me.ComboBox1.Text & "')" 

    cmd.ExecuteNonQuery() 

    'refresh data in list 

    'close connection 
    con.Close() 
End If 
+1

你得到什麼錯誤? – stinepike

+0

[將數據從VB.NET插入MS Access:INSERT INTO語句中的語法錯誤]可能的重複(http://stackoverflow.com/questions/20808528/inserting-data-from-vb-net-to-ms-access -syntax-error-in-insert-into-statement) – Bugs

+0

看看鏈接。所提供的答案應該給你後面的東西。使用參數。而且我會像方括號一樣將列包裝成'[username],[password],[typeofuser]'。 – Bugs

回答

1

首先,你不打開連接:

con.Open() 

接下來,passwordreserved word in MS Access。您需要在方括號包裹password

[password] 

您在連接字符串,而不是使用PARAMATERS的:在給你TextBoxComboBox控制一個合適的名字,而不是使用TextBox1

cmd.Parameters.Add("@username", OleDbType.VarChar).Value = txtUsername.Text 
cmd.Parameters.Add("@password", OleDbType.VarChar).Value = txtPassword.Text 
cmd.Parameters.Add("@typeofuser", OleDbType.VarChar).Value = cmbTypeOfUser.Text 

看,TextBox2ComboBox1。這有助於正確地識別各個控制:

txtUsername 
txtPassword 
cmbTypeOfUser 

移動使用MsgBox和使用MessageBox.Show了。 MsgBox存在VB6並最終委託給MessageBox反正這麼有意義的使用MessageBox.Show

MessageBox.Show("No Username and Password inserted") 

最後我會考慮實施Using這將有助於密切處置您的SQL對象的

Using cmd As New OleDbCommand(command, connection) 

End Using 

總之您的代碼會是這個樣子:

If txtUsername.Text = Nothing And txtPassword.Text = Nothing Then 

    MessageBox.Show("No Username and Password inserted") 
    TextBox1.Focus() 

Else 

    Using con As New OleDbConnection(connectionString), 
      cmd As New OleDbCommand("INSERT INTO [loginTable] ([username], [password], [typeofuser]) VALUES (@username, @password, @typeofuser)", con) 

     con.Open() 

     cmd.Parameters.Add("@username", OleDbType.VarChar).Value = txtUsername.Text 
     cmd.Parameters.Add("@password", OleDbType.VarChar).Value = txtPassword.Text 
     cmd.Parameters.Add("@typeofuser", OleDbType.VarChar).Value = cmbTypeOfUser.Text 

     cmd.ExecuteNonQuery() 

    End Using 

End If 

這是超出了這個問題的範圍,但我也看看加密密碼。將它們存儲爲純文本是不好的做法。看看這個SO問題; Best way to store password in database,這可能會給你一些關於如何最好地做到這一點的想法。

0

有(至少)幾個方法可以做到這一點。所以,試試這個。 。 。

Imports System.Data.OleDb 

Public Class Form1 

    Private ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Excel\Desktop\Coding\Microsoft Access\Northwind.mdb;" 
    Private NewIdentifer As Integer = 0 
    Private InsertStatement As String = "INSERT INTO Employee (LName) Values(@LName)" 
    Private IdentifierStatement As String = "Select @@Identity" 

    'Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     Using cn As New OleDbConnection(ConnectionString) 
      Using cmd As New OleDbCommand("SELECT * FROM Employee", cn) 
       Dim dt As New DataTable 
       cn.Open() 
       Dim Reader As OleDbDataReader = cmd.ExecuteReader() 
       dt.Load(Reader) 
       Dim dv = dt.DefaultView 
       DataGridView1.DataSource = dv 
      End Using 
     End Using 
    End Sub 

    'End Sub 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 

     If Not String.IsNullOrEmpty(txtLastName.Text) Then 
      Using cn As New OleDbConnection(ConnectionString) 
       Using cmd As New OleDbCommand(InsertStatement, cn) 
        cmd.Parameters.AddWithValue("@LName", txtLastName.Text) 
        cn.Open() 
        cmd.ExecuteNonQuery() 
        cmd.CommandText = IdentifierStatement 
        NewIdentifer = CInt(cmd.ExecuteScalar()) 
        Dim Row As DataRowView = CType(DataGridView1.DataSource, DataView).AddNew 
        Row("Fname") = NewIdentifer 
        Row("LName") = txtLastName.Text 
        Row.EndEdit() 
        DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.RowCount - 1) 
        txtLastName.Text = "" 
       End Using 
      End Using 
     Else 
      MsgBox("Please enter a name") 
     End If 

    End Sub 
End Class 

另外,試試。 。 。

Imports System.Data.OleDb 

Public Class Form1 



    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 

     ' Requires: Imports System.Data.OleDb 

     ' ensures the connection is closed and disposed 
     Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
      "Data Source=""C:\Users\Ryan\Desktop\Coding\DOT.NET\Samples VB\Insert Into MS Access Table from Textbox\WindowsApplication1\bin\InsertInto.mdb"";" & _ 
      "Persist Security Info=False") 
      ' open connection 
      connection.Open() 

      ' Create command 
      Dim insertCommand As New OleDbCommand(_ 
       "INSERT INTO Table1([inputOne] , [inputTwo] , [inputThree]) " & _ 
       "VALUES (@inputOne, @inputTwo, @inputThree);", _ 
       connection) 
      ' Add the parameters with value 
      insertCommand.Parameters.AddWithValue("@inputOne", TextBox1.Text) 
      insertCommand.Parameters.AddWithValue("@inputTwo", TextBox2.Text) 
      insertCommand.Parameters.AddWithValue("@inputThree", TextBox3.Text) 
      ' you should always use parameterized queries to avoid SQL Injection 
      ' execute the command 
      insertCommand.ExecuteNonQuery() 

      MessageBox.Show("Insert is done!!") 

     End Using 

    End Sub 
End Class