问题
So im trying to add fields to a database. It is .mdb database, microsoft access.
The Name of the table is Contacts.
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source= C:\Users\Owner\Desktop\Contacts.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "INSERT INTO Contacts (FName, LName, Age, Address Line 1, Address Line 2, City, State, Zip, Home Phone, Work Phone, Email, Sex) VALUES (a, b, c,d,e,f,g,h,i,j,k)"
da = New OleDb.OleDbDataAdapter(Sql, con)
da.Fill(ds, "Contacts")
My Error is Syntax error in INSERT INTO statement. Which makes no sense, whatsoever. What am i doing wrong?
EDIT* I solvedmy riginal problem by adding [] around certian fields as suggested, thanks. Now I am getting...
No value given for one or more required parameters.
The database has a primary ID field that autoincrements, does this change anything?
回答1:
As other answers have already explained you need to use square brackets around column names that contain spaces, but also you need to add a value for the fields otherwise you cannot execute the command.
I will try to show a complete example
Dim dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
Dim dbSource = "Data Source= C:\Users\Owner\Desktop\Contacts.mdb"
Dim sql = "INSERT INTO Contacts (FName, LName, Age, " & _
"[Address Line 1], [Address Line 2], City, State, Zip, " & _
"[Home Phone], [Work Phone], Email, Sex) " & _
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
Using con = New OleDb.OleDbConnection(dbProvider & dbSource)
Using cmd = new OleDb.OleDbCommand(sql, con)
con.Open()
cmd.Parameters.AddWithValue("@p1", "Value For FName")
cmd.Parameters.AddWithValue("@p2", "Value For LName")
cmd.Parameters.AddWithValue("@p3", Convert.ToInt32("Value For Age"))
.... and so on for the other parameters .....
cmd.ExecuteNonQuery()
End Using
End Using
In this example I have inserted 12 placeholders for the parameters (?) and then added the first 3 parameters out of 12 required. Note that with OleDb the parameter collection still requires to add the parameters with a name (@pX), but when executing the query the parameter value is picked following the same order of the placeholder.
Also I have used the Using statement to close and dispose the disposable objects like the connection and the command.
Finally, an Insert query is normally executed using ExecuteNonQuery from the OleDbCommand and there is no need to use an OleDbAdapter and call Fill to load a DataSet when no SELECT query is executed
回答2:
Put column names with spaces between squares brackets []
For example [Address Line 1]
Cheers
回答3:
Tables and fields names composed of a single word can be written directly, such as FName
.
Names like Address Line 1
howover will need to be wrapped between []
in order to make the synthax consistent.
INSERT INTO Contacts (FName, LName, Age, [Address Line 1], [Address Line 2], City, State, Zip, [Home Phone], [Work Phone], Email, Sex) VALUES (...
The same applies to objects named with reserved words.
回答4:
If I understood what your needs are correctly:
Translated from : Se entendi o que está precisando é isto aqui,veja o código:
comando = New OleDbCommand("INSERT INTO TabClientes(NomeCliente,Telefone,CEP,Rua,Bairro,Cidade,Estado)" & _
"Values (@nome,@telefone,@cep,@rua,@bairro,@cidade,@estado)")
comando.Connection = conn
comando.Parameters.AddWithValue("@nome", txtnome.Text)
comando.Parameters.AddWithValue("@telefone", mkbtelefone.Text)
comando.Parameters.AddWithValue("@cep", mkbcep.Text)
comando.Parameters.AddWithValue("@rua", txtrua.Text)
comando.Parameters.AddWithValue("@bairro", txtbairro.Text)
comando.Parameters.AddWithValue("@cidade", txtcidade.Text)
comando.Parameters.AddWithValue("@estado", txtestado.Text)
' comando.ExecuteNonQuery()
If comando.ExecuteNonQuery Then
MessageBox.Show("Registro salvo com sucesso ...")
Else
MessageBox.Show("Erro ao tentar gravar o registro ...")
End If
conn.Close()
来源:https://stackoverflow.com/questions/21961951/inserting-ms-access-row-into-database-using-vb-net