Connect To SQL Server With Visual Basic
Connect To SQL Server With Visual Basic
The first thing is to open your new project with Visual Basic, add a Button to the form.
y unDataGridView, después en la misma ventana de diseño damos un doble clic sobre
the button to show us the code screen.
First of all, we declare the Imports, we declare this at the beginning of everything.
code:
(Please, if you liked this, go to the top right column on this page and click on)
+1)
Imports System.Data
Imports System.Data.SqlClient
Subsequently, within the Click event of the button, we will start with the code that
will connect and display the data:
First, we create a variable called cnn of the type SQL connection and we will assign
How to evaluate the connection string to our SQL Server database:
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
The Fill method is responsible for creating the tables and assigning the column names and the
data types that will store those columns, as well as filling the rows with the
data based on the result obtained with the SQL query created for the property
SelectCommand. In this way, it fills the indicated DataSet and its DataTables.
All the complete code:
1
2
Imports System.Data
3
Imports System.Data.SqlClient
4
5
Public Class Form1
6
7
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
8
System.EventArgs) Handles Button1.Click
9
Dim cnn As New SqlConnection("Server=valp-
1
maple01;uid=sa;pwd=messrv;database=Wasp
0
Dim da As New SqlDataAdapter("select * from optimas", cnn)
1
Dim ds As New DataSet
1
da.Fill(ds)
1
DataGridView1.DataSource = ds.Tables(0)
2
End Sub
1
3
End Class
1
4
The Fill method is responsible for creating the tables and giving them the column names and the
data types that will store those columns, as well as filling the rows with the
data based on the result obtained from the SQL query created for the property
SelectCommand. In this way, it fills the indicated DataSet and its DataTables. The types
of data that assigns to each column follow the following rules of agreement that
you can see in the following table:
Here is the example for downloading, just change the connection string for the one from
your data is ready!