0% found this document useful (0 votes)
10 views3 pages

Connect To SQL Server With Visual Basic

The document provides a tutorial on connecting to SQL Server using Visual Basic .NET and displaying data in a DataGridView. It includes code snippets for establishing a connection, executing a SQL query, and filling a DataSet with the results. A downloadable example is also mentioned, with instructions to modify the connection string as needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Connect To SQL Server With Visual Basic

The document provides a tutorial on connecting to SQL Server using Visual Basic .NET and displaying data in a DataGridView. It includes code snippets for establishing a connection, executing a SQL query, and filling a DataSet with the results. A downloadable example is also mentioned, with instructions to modify the connection string as needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Connect to SQL server with Visual Basic.

net and display


data
A small example of how to connect to SQL Server and display data with a dataset
through a select query. This way we can easily create an SQL Database from Forms
with VB & SQL (Below is the link to download the example in Visual Studio solution)

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:

Dim cnn As New


SqlConnection("Server=MyServer;uid=sa;pwd=123456;database=MyDB")
Then we define a variable of type Sql Adapter to which we will assign a value the
SQL query string that we want to execute, that is, the Query, to this string the
we will call it

Dim da As New SqlDataAdapter("select * from optimas", cnn)

We define another variable called ds of type DataSet:

1 Dim ds As New DataSet

and then we call the data:

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!

You might also like