Visual Basic Sql Code Example

/ Comments off
Visual Basic Sql Code Example Average ratng: 3,6/5 9857 votes
Sql
  1. Visual Basic Sql Connection

Connecting to a SQL Server DatabaseTo read and write information to and from aSQL Server database, it is necessary first to establish a connectionto the database. This is done with theSqlConnection object, found in theSystem.Data.SqlClient namespace.Here’s an example:' Open a database connection.Dim strConnection As String = 'Data Source=localhost;Initial Catalog=Northwind;' & 'Integrated Security=True'Dim cn As SqlConnection = New SqlConnection(strConnection)cn.Open( )This code fragment instantiates an object of type SqlConnection,passing its constructor a connection string. Calling theSqlConnection object’s Open method opens the connection.

Visual Basic Sql Connection

Visual

In this article. Use the following code examples to learn how to use the ADO methods, properties, and events when writing in Visual Basic. Paste the entire code example, from Sub to End Sub, into your code editor. The example may not run correctly if partial examples are used or if paragraph formatting is lost.

Aconnection must be open for data to be read or written, or forcommands to be executed. When you’re finished accessing thedatabase, use the Close method to close the connection:' Close the database connection.cn.Close( )The connection string argument to the SqlConnection class’sconstructor provides information that allows the SqlConnection objectto find the SQL Server database. The connection string shown in theearlier code fragment indicates that the database is located on thesame machine that is running the code snippet( Data Source=localhost), thatthe database name is Northwind ( Initial Catalog=Northwind), and that the user ID thatshould be used for logging in to SQL Server is the current Windowslogin account ( Integrated Security=True). Shows the valid SQL Server connection stringsettings.