Download presentation
Presentation is loading. Please wait.
Published byEmory Matthews Modified over 8 years ago
1
Lecture 7
2
1. Select Operation 2. Insert Operation 3. Update Operation 4. Delete Operation
3
Dim connetionString As String Dim cnn As SqlConnection Dim cmd As SqlCommand Dim sql As String // var for command Dim reader As SqlDataReader connetionString = "Data Source=.\SqlExpress;Initial Catalog=my_database;Integrated Security=True“ sql = "select * from table_1“ cnn = New SqlConnection(connetionString) Try cnn.Open() cmd = New SqlCommand(sql, cnn) reader = cmd.ExecuteReader() While reader.Read() MsgBox(reader.Item(0) & " - " & reader.Item(1) & " - " & reader.Item(2)) End While reader.Close() cmd.Dispose() cnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try
4
The ExecuteReader method call is suitable when the underlying command object is set with the Select statement. Select statement will return one or more records and those records are packed in SqlDataReader by the ExecuteReader method and then returned to the caller. cmd.Dispose() Dispose releases all the resources used by the object.
5
SQL Adapter: Initializes a new instance of the SqlDataAdapter class with a SelectCommand and a SqlConnection object. Provides the communication between the Dataset and the Datasource. That is the DataAdapter uses the Select statements to fill a DataSet ▪ and use the other three SQL commands (Insert, Update, delete) to transmit changes back to the Database.
6
Dim connetionString As String Dim connection As SqlConnection Dim adapter As New SqlDataAdapter Dim sql As String connetionString = "Data Source=.\SqlExpress;Initial Catalog=my_database;Integrated Security=True“ connection = New SqlConnection(connetionString) sql = "insert into table_1 (id,name,batch) values(3,'Babar','2k13')“ Try connection.Open() adapter.InsertCommand = New SqlCommand(sql, connection) adapter.InsertCommand.ExecuteNonQuery() MsgBox("Row inserted !! ") Catch ex As Exception ▪ MsgBox(ex.ToString) End Try
7
Dim connetionString As String Dim connection As SqlConnection Dim adapter As New SqlDataAdapter Dim sql As String connetionString = "Data Source=.\SqlExpress;Initial Catalog=my_database;Integrated Security=True" connection = New SqlConnection(connetionString) sql = "update table_1 set name = 'zeeshan' where id = 3“ Try connection.Open() adapter.UpdateCommand = connection.CreateCommand adapter.UpdateCommand.CommandText = sql adapter.UpdateCommand.ExecuteNonQuery() MsgBox("Row updated !! ") Catch ex As Exception MsgBox(ex.ToString) End Try
8
Dim connetionString As String Dim connection As SqlConnection Dim adapter As New SqlDataAdapter Dim sql As String connetionString = "Data Source=.\SqlExpress;Initial Catalog=my_database;Integrated Security=True“ connection = New SqlConnection(connetionString) sql = "delete table_1 where id =4“ Try connection.Open() adapter.DeleteCommand = connection.CreateCommand adapter.DeleteCommand.CommandText = sql adapter.DeleteCommand.ExecuteNonQuery() MsgBox("Row(s) deleted !! ") Catch ex As Exception MsgBox(ex.ToString) End Try
9
Assignment: Write a calculator program in VB.Net. Assignment: Create web browser program with back button. Assignment: Write short description of VB.net controls covered in class (Handwritten). Assignment: What are advantages of VB.net in comparison with Visual Basic 6? Assignment: Create a database of your class and retrieve it on Gridview.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.