我需要帮助,我开发了一个简单的Access数据库,并创建了一个VB .Net用户界面。 我无法将数据插入表格这里是我的示例代码...我只是VB.Net的初学者我希望你能帮助我这个
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim dr As DataRow
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Documents and Settings\Test\Desktop\Privilege.Net\PrivilegeCard\Database\dbPrivilegeSystem.mdb"
cmd.CommandText = "Select * From tblUsers;"
Try
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.Text
da.SelectCommand = cmd
da.Fill(ds, "tblUsers")
dr = ds.Tables("tblUsers").NewRow
'System.Convert.ToDateTime(dtPicker.Value).ToOADate()
'I've been trying to use this conversion for date thinking if this could help me fix my problem
dr(0) = txtUserN.Text
dr(1) = txtPass.Text
dr(2) = txtAccess.Text
dr(3) = dtPicker.Value
dr(4) = txtName.Text
dr(5) = txtPos.Text
dr(6) = cmbDept.Text
ds.Tables("tblUsers").Rows().Add(dr)
Dim cb As New OleDbCommandBuilder(da)
da.Fill(ds)
da.Update(ds, "tblUsers")
Catch ex As OleDbException
MessageBox.Show(ex.Message & " - " & ex.Source)
End Try
答案 0 :(得分:1)
看看我的代码,它更加轻松
Imports System.Data
Partial Class theClassName
Inherits System.Web.UI.Page
Protected Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button.Click
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=" & MapPath("~/App_Data") & "/database.mdb"
Dim dbconnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
Dim queryString As String = "Insert into [customer] ([ID],[name],[address],[phone],[date_of_birth],[Nationality],[room_no],[number_of_days]) Values (" & txtid.Text & ", '" & txtname.Text & "', '" & txtaddress.Text & "', " & txtphone.Text & ", " & txtdate.Text & ", '" & txtnation.Text & "'," & txtroom.Text & "," & txtday.Text & ")"
Dim dbcommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbcommand.CommandText = queryString
dbcommand.Connection = dbconnection
dbconnection.Open()
Dim rowsAffected As Integer = 0
Try
rowsAffected = dbcommand.ExecuteNonQuery
result.Text = "Record Saved"
Catch
result.Text = "Record not saved"
Finally
dbconnection.Close()
End Try
End Sub
End Class
答案 1 :(得分:0)
我昨天在这上花了几个小时,并且还得到了无法解释的语法错误。如果您仍想使用DataAdapter和CommandBuilder,请尝试调整以下内容以适合您的数据库。首先,这是一个好主意,并且可能必须使其工作,以创建唯一/自动数字ID列。此外,将PassWord字段名称更改为不会冒关键字冲突的风险。最后,我在某处看到带有空格的字段名称在这些情况下也可能不起作用。
这对我有用,希望它适合你! (而且你的日期有效。)
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim dr As DataRow
Dim cb As OleDbCommandBuilder
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Client.mdb"
Try
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select * from Client;"
da = New OleDbDataAdapter(cmd)
cb = New OleDbCommandBuilder(da)
da.Fill(ds, "Client")
dr = ds.Tables("Client").NewRow()
'System.Convert.ToDateTime(dtPicker.Value).ToOADate()
'I've been trying to use this conversion for date thinking if this could help me fix my problem
'dr(0) = Auto Index. Don't update.
dr(1) = "updated"
dr(2) = 345
ds.Tables("Client").Rows.Add(dr)
da.Update(ds, "Client")
con.Close()
Catch ex As OleDbException
MessageBox.Show(ex.Message & " - " & ex.Source)
End Try
答案 2 :(得分:0)
抱歉迟到的回复...... 您的代码中没有错误,但您仍然在INSERT INTO语句中出错,因为MS Access 严格使用的关键字,如密码,时间戳,用户,位置;避免将错误更改字段转换为userpassword或userpwd或任何唯一字段。
谢谢...