manipulating database
Before coding the SQL for database manipulation, declarations are added to the code:
Dim connection As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("connString").ConnectionString)
Dim command As New Data.SqlClient.SqlCommand
Dim reader As Data.SqlClient.SqlDataReader
The connString variable can be updated on the web.config file. Refer to this for a review on connection string.
Now, to insert data into a database table:
command = New Data.SqlClient.SqlCommand("INSERT INTO TABLE (COLUMN_1, COLUMN_2, COLUMN_3) VALUES (@COLUMN_1, @COLUMN_2, @COLUMN_3)", connection)
command.Parameters.Add("@COLUMN_1", Data.SqlDbType.VarChar).Value = textBox1.Text
command.Parameters.Add("@COLUMN_2", Data.SqlDbType.VarChar).Value = textBox2.Text
command.Parameters.Add("@COLUMN_3", Data.SqlDbType.VarChar).Value = textBox3.Text
command.ExecuteNonQuery()
Likewise, data inserted can also be displayed on asp.net controls:
command = New Data.SqlClient.SqlCommand("SELECT * FROM TABLE", connection)
reader = command.ExecuteReader()
If reader.Read() Then
textBox1.Text = reader("COLUMN_1")
textBox2.Text = reader("COLUMN_2")
textBox3.Text = reader("COLUMN_3")
Else
End If
reader.Close()
Both actions would require you to open your connection by:
connection.Open()
And then closing it after using:
connection.Close()