[英]Insert data into SQL database in VB.NET
I am trying to insert Supplier data into SQL database which I built inside VB.NET. 我试图将供应商数据插入我在VB.NET内部构建的SQL数据库中。
Here is the code that I am using: 这是我正在使用的代码:
Dim myconnect As New SqlClient.SqlConnection
myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB\InvDB.mdf;Integrated Security=True;User Instance=True"
Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
mycommand.Connection = myconnect
mycommand.CommandText = "INSERT INTO Supplier (SupplierID, Name, Email,Phone,Address,City) VALUES (@SID, @Name, @Email, @Phone, @Address, @City)"
myconnect.Open()
Try
mycommand.Parameters.Add("@SID", SqlDbType.Int).Value = SIDTextBox.Text
mycommand.Parameters.Add("@Name", SqlDbType.NVarChar).Value = NameTextBox.Text
mycommand.Parameters.Add("@Email", SqlDbType.VarChar).Value = EmailTextBox.Text
mycommand.Parameters.Add("@Phone", SqlDbType.VarChar).Value = PhoneTextBox.Text
mycommand.Parameters.Add("@Address", SqlDbType.NVarChar).Value = AddressTextBox.Text
mycommand.Parameters.Add("@City", SqlDbType.NVarChar).Value = CityTextBox.Text
mycommand.ExecuteNonQuery()
MsgBox("Success")
Catch ex As System.Data.SqlClient.SqlException
MsgBox(ex.Message)
End Try
myconnect.Close()
The problem when I enter the data, I got the Success message but when I check the database, I can't find the data !! 输入数据时出现问题,我收到了成功消息,但是当我检查数据库时,找不到数据!
I checked the mdf file and I found it correct (The same one that I am using) 我检查了mdf文件,发现它正确(我使用的是同一文件)
Any help please. 请帮忙。
I got it 我知道了
The problem was with this line: 问题出在这一行:
myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB\InvDB.mdf;Integrated Security=True;User Instance=True"
I replaced |DataDirectory|
我替换了|DataDirectory|
with the path of the InvDB.mdf
and it worked 与InvDB.mdf
的路径,它工作
Dim query as String = String.Empty
query = "INSERT INTOSupplier (SupplierID, Name, Email,Phone,Address,City) VALUES (@SID, @Name, @Email, @Phone, @Address, @City) "
Using conn as New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB\InvDB.mdf;Integrated Security=True;User Instance=True")
Using comm As New SqlCommand()
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
mycommand.Parameters.Add("@SID", SqlDbType.Int).Value = SIDTextBox.Text
.Parameters.Add("@Name", SqlDbType.NVarChar).Value = NameTextBox.Text
.Parameters.Add("@Email", SqlDbType.VarChar).Value = EmailTextBox.Text
.Parameters.Add("@Phone", SqlDbType.VarChar).Value = PhoneTextBox.Text
.Parameters.Add("@Address", SqlDbType.NVarChar).Value = AddressTextBox.Text
.Parameters.Add("@City", SqlDbType.NVarChar).Value = CityTextBox.Text
End With
Try
conn.open()
comm.ExecuteNonQuery()
Catch(ex as SqlException)
MessageBox.Show(ex.Message.ToString(), "Error Message")
End Try
End Using
End USing
Try this out. 试试看
我遇到了类似的问题,在我的情况下,应用程序没有对数据库文件所在文件夹的写访问权。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.