Click here to Skip to main content
15,943,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi can you tell me how to connect visual basic 2010 with sql2008 r2 database and also add,retrieve
data from sql 2008 r2 database?
Posted

1 solution

There are loads of ways.
Here is the most basic.
Retrieve info:
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("SELECT iD, description FROM myTable", con)
		Using reader As SqlDataReader = com.ExecuteReader()
			While reader.Read()
				Dim id__1 As Integer = CInt(reader("iD"))
				Dim desc As String = DirectCast(reader("description"), String)
				Console.WriteLine("ID: {0}" & vbLf & "    {1}", iD, desc)
			End While
		End Using
	End Using
End Using
Insert new rows:
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
		com.Parameters.AddWithValue("@C1", myValueForColumn1)
		com.Parameters.AddWithValue("@C2", myValueForColumn2)
		com.ExecuteNonQuery()
	End Using
End Using
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900