{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
// TODO Build connection string
Maybe you are interested!
-
Database - Hanoi Industrial College - 2 -
SQL server database management system course, web design profession, Dalat College of Technology - 14 -
Database Programming - Hanoi Industrial Vocational College - 22 -
Database Programming - Hanoi Industrial Vocational College - 1 -
Database Programming - Hanoi Industrial Vocational College - 14
conn.ConnectionString =
"integrated security=true;data source=name_SQLSERVER;" + "persist security info=False;initial catalog=database_name";

try
{
conn.Open();
// Build code to interact with database here
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
finally
{
conn.Close();
}
}
In VB.NET
Public Sub ConnectToSql()
Dim conn As New SqlClient.SqlConnection ' TODO Build connection string
conn.ConnectionString = & _
"integrated security=true;data source=name_SQL Server;" & _ "persist security info=False;initial catalog=database_name"
Try
conn.Open()
' Build code to interact with the database here
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
Finally
conn.Close() End Try
End Sub
In the two examples above, we build a connection function into SQL Server 2005, the most important component of which is the connection string to the database:
In C#:
"integrated security=true;data source=name_SQLSERVER;" + "persist security info=False;initial catalog=database_name";
In VB.NET:
"integrated security=true;data source=name_SQL Server;" & _ "persist security info=False;initial catalog=database_name"
In there:
Integrated security = true : use Windows Authentication
data source : specifies the name of the SQL Server 2005 instance we want to connect to.
persist security info : The default setting for the persist security info keyword is false. Setting it to true will allow sensitive data including UserID and password to be retrieved when the connection is open.
initial catalog : The name of the database we want to interact with. For example, the connection string using Windows Authentication: "integrated security=true;data source=.\SQLExpress" +
"persist security info=False;initial catalog=myDB";
Example connection string using SQL Server Authentication:
"persist security info=False;User ID = *****; password = ***** “ +
“initial catalog=myDB; data source=.SQLExpress ";
8.2.2 VB 6
The example below illustrates building a database application using VB6. Suppose we have
Database name is Test, SQL Server is .\SQLExpress, User is sa, Password is 1234.
Private Sub Command1_Click() Dim connectionString As String Dim commandString As String
Dim sqlConnection As ADODB.Connection
Dim rs As Recordset
connectionString="PROVIDER=SQLOLEDB; DATA SOURCE=.SQLEXPRESS;”
connectionString = “UID=sa; PWD=1234;DATABASE=Test" commandString ="select count(*) as count from Users where UserName ='" commandString = commandString & Text1.Text & "' and Password ='" commandString = commandString & Text2.Text & "' "
Set sqlConnection = New ADODB.Connection sqlConnection.Open(connectionString)
Set rs = New Recordset
rs.Open commandString, sqlConnection
If (rs("count") = 1) Then MsgBox "Login successfully"
End If End Sub
References
1. SQL Server database management system textbook, Faculty of Information Technology, Hue University.
2. SQL Server 2005, T-SQL Recipes: Problem, Solution, Approach – Apppress Publisher.
3. Sams Teach yourself Microsoft SQL Server 2005 Express in 24 hours.





