הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, April 28, 2015 5:20 PM
hi,
I'm new to database programming in vb. I just create a database in sql server 2008 and I make all connections,bindings in vb form.
I dragged table to form. Now I want to know insert,delete update database by using values in textbox .I want to do through code.
please help!.
All replies (3)
Tuesday, April 28, 2015 7:03 PM ✅Answered
Hello,
The following is for MS-Access, a full working solution, by changing from OleDb to SqlClient data provider the same logic applies to SQL-Server
With the above, the following I did shows a read and insert on SQL-Server example
https://code.msdn.microsoft.com/Adding-new-records-into-bff5eaaf
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
Wednesday, April 29, 2015 8:21 AM ✅Answered
Hi KanugoluBullibau,
I supposed that you have three fields in the database, Id, Date, Demo. Id is primary key.
In the form, have three textBoxs, named "TextBox1","TextBox2" and "TextBox3".
'Insert a new record.
Private Sub Insert_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim constr = "data source=WXPFSER12-LAB1;initial catalog=PlatformDB;Integrated Security=True"
Dim con As SqlConnection = New SqlConnection(constr)
Dim sql = "INSERT INTO [sss] (Id , Date , Demo ) VALUES (@Id , @Date , @Demo)"
Dim sda As SqlDataAdapter = New SqlDataAdapter(sql, constr)
Dim com As SqlCommand = New SqlCommand(sql, con)
Try
con.Open()
com.Parameters.AddWithValue("@Id", TextBox1.Text)
com.Parameters.AddWithValue("@Date", TextBox2.Text)
com.Parameters.AddWithValue("@Demo", TextBox3.Text)
com.ExecuteNonQuery()
MsgBox("Insert success")
Catch ex As Exception
Finally
con.Close()
End Try
End Sub
'Update the row whose Id is equal to TextBox1.Text.
Private Sub Update_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim constr = "data source=WXPFSER12-LAB1;initial catalog=PlatformDB;Integrated Security=True"
Dim con As SqlConnection = New SqlConnection(constr)
Dim sql = "UPDATE sss SET Date=@Date,Demo=@Demo WHERE Id= @Id"
Dim sda As SqlDataAdapter = New SqlDataAdapter(sql, constr)
Dim com As SqlCommand = New SqlCommand(sql, con)
Try
con.Open()
com.Parameters.AddWithValue("@Id", TextBox1.Text)
com.Parameters.AddWithValue("@Date", TextBox2.Text)
com.Parameters.AddWithValue("@Demo", TextBox3.Text)
com.ExecuteNonQuery()
MsgBox("Update success")
Catch ex As Exception
Finally
con.Close()
End Try
End Sub
'Delete the record whose Id= TextBox1.Text
Private Sub Delete_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim constr = "data source=WXPFSER12-LAB1;initial catalog=PlatformDB;Integrated Security=True"
Dim con As SqlConnection = New SqlConnection(constr)
Dim sql = "Delete From sss WHERE Id= @Id"
Dim sda As SqlDataAdapter = New SqlDataAdapter(sql, constr)
Dim com As SqlCommand = New SqlCommand(sql, con)
Try
con.Open()
com.Parameters.AddWithValue("@Id", TextBox1.Text)
com.ExecuteNonQuery()
MsgBox("Delete success")
Catch ex As Exception
Finally
con.Close()
End Try
End Sub
If you have any other concern regarding this issue, please feel free to let me know.
Best regards,
Youjun Tang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Wednesday, April 29, 2015 3:59 PM ✅Answered
From where did you drag the table? Was it from the DataSources? If so, the SQL statements were probably generated when you created the data source (TableAdapter). When you dragged it to the Form a BindingNavigator and BindingSource would have been created that binds the data source to the DataGridView (or TextBoxes if you dragged the column names to the Form). The buttons on the BindingNavigator uses the BindingSource to run the SQL statements that are in your data source (probably a TableAdapter) and updates the DataGridView (or TextBoxes) accordingly:
Database table -> DataSet -> TableAdapter -> BindingSource -> BindingNavigator -> DataGridView
So bottom line, if you are looking for the SQL INSERT, DELETE and UPDATE statements you will find them in the TableAdapter.
Paul ~~~~ Microsoft MVP (Visual Basic)