これはいくつかの方法があります。だから、これを試してみてください。 。 。
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
どのようなエラーが表示されますか? – stinepike
[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
リンクを見てください。提供される答えは、あなたが何をしているのかを示すはずです。パラメータを使用します。そして、 '[username]、[password]、[typeofuser]'のように大括弧で囲んでいます。 – Bugs