我试图创建一个用户并插入到Access数据库中,但是当我单击按钮时出现错误“ connectionString属性尚未初始化”
我已经在连接字符串上尝试了所有可能的代码,但是挑战仍然存在。
Try
Dim sqlconn As New OleDb.OleDbConnection
Dim sqlquerry As New OleDb.OleDbCommand
Dim connString As String
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PavilionDB.mdb"
sqlquerry.Connection = sqlconn
con = New OleDbConnection(conString)
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
sqlquerry.CommandText = "INSERT INTO member(mmbr_id, name, gender, address, phone, join_date, acc_no) VALUES (@txtNewID, @txtName, @txtGender, @txtAddress, @txtPhone, @txtPhone, @)txtAccNo"
sqlquerry.Parameters.AddWithValue("New ID", txtNewID.ToString)
sqlquerry.Parameters.AddWithValue("Name", txtName.ToString)
sqlquerry.Parameters.AddWithValue("Gender", txtGender.ToString)
sqlquerry.Parameters.AddWithValue("Address", txtAddress.ToString)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataTable
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "con")
Return
sqlquerry.ExecuteNonQuery()
sqlconn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
答案 0 :(得分:0)
该代码确实是一团糟。应该减少到这个数目,这样才能解决您的问题:
Try
Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PavilionDB.mdb"),
command As New OleDbCommand("INSERT INTO member (mmbr_id, name, gender, address, phone, join_date, acc_no) VALUES (@mmbr_id, @name, @gender, @address, @phone, @join_date, @acc_no)", connection)
With command.Parameters
.AddWithValue("@mmbr_id", txtNewID.Text)
.AddWithValue("@name", txtName.Text)
.AddWithValue("@gender", txtGender.Text)
.AddWithValue("@address", txtAddress.Text)
'Set other parameters here.
End With
connection.Open()
command.ExecuteNonQuery()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
理想情况下,您也会使用Add
而不是AddWithValue
,但这是另一回事。
答案 1 :(得分:0)
这里的问题是您的代码中有2个命令,分别为cmd
和sqlquerry
。您可以在代码的开头定义sqlqeurry,然后使用连接和我无法确定其来源的sql变量初始化cmd
。
您需要做的是重做这一部分,并删除似乎是问题根源的多余参考。请考虑以下...
Try
Dim conString As String
Dim text = "INSERT INTO member(mmbr_id, name, gender, address, phone, join_date, acc_no) VALUES (@txtNewID, @txtName, @txtGender, @txtAddress, @txtPhone, @txtPhone, @)txtAccNo"
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PavilionDB.mdb"
Using sqlCon = New OleDbConnection(conString), sqlCmd = New OleDbCommand(text, sqlCon)
With sqlCmd.Parameters
.AddWithValue("New ID", 234234)
.AddWithValue("Name", "Fabulous")
.AddWithValue("Gender", "Male")
.AddWithValue("Address", "127.0.0.1")
End With
sqlCon.Open()
sqlCmd.ExecuteNonQuery()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
我无法确定txtNewID和其他类似命名的变量是否为文本框。如果它们是文本框,则需要.Text
属性,该属性以字符串形式获取内容。您需要替换我在测试环境中使用的文字来实现它。
确保所有内容(包括连接字符串和查询)均正确无误,以便顺利运行。在当前情况下,由于尝试对未与该连接关联的连接执行查询,因此出现与连接字符串相关的错误。
答案 2 :(得分:0)
除了此链接上的整个开放源代码之外,这里还有一些简单的代码可满足您的主要要求: enter link description here
这是经过测试的代码:
Private Sub BtnAddNewRec_Click(sender As Object, e As EventArgs) Handles BtnAddNewRec.Click
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Try
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Tbl1 (mmbr_id, name, gender, address) VALUES (@txtNewID, @txtName, @txtGender, @txtAddress)"
cmd.Parameters.AddWithValue("@txtNewID", txtNewID.Text)
cmd.Parameters.AddWithValue("@txtName", txtName.Text)
cmd.Parameters.AddWithValue("@txtGender", txtGender.Text)
cmd.Parameters.AddWithValue("@txtAddress", txtAddress.Text)
cmd.ExecuteNonQuery()
MsgBox("Record Added Successfully!")
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
End Sub
我希望这会有所帮助: