0% found this document useful (0 votes)
4 views

SQL_Server_Connection

This document provides a step-by-step guide for connecting SQL Server with Visual Studio 2010. It includes instructions on creating a new project, configuring the connection in Server Explorer, testing the connection, and using C# code to establish the connection. The guide emphasizes selecting the appropriate data source and authentication method for successful connectivity.

Uploaded by

rogitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL_Server_Connection

This document provides a step-by-step guide for connecting SQL Server with Visual Studio 2010. It includes instructions on creating a new project, configuring the connection in Server Explorer, testing the connection, and using C# code to establish the connection. The guide emphasizes selecting the appropriate data source and authentication method for successful connectivity.

Uploaded by

rogitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL Server Connection with Visual Studio 2010

1. Open Visual Studio 2010


• Launch Visual Studio 2010 and create a new project.
• Select 'Windows Forms Application' or 'ASP.NET Web Application', depending on your
requirement.

2. Open Server Explorer


• Go to 'View' → 'Server Explorer'.
• Right-click on 'Data Connections' → Select 'Add Connection'.

3. Configure Connection
• In the 'Add Connection' window:
- Select 'Microsoft SQL Server (SqlClient)' as the data source.
- Enter the 'Server Name' (e.g., .\SQLEXPRESS for local SQL Server Express).
- Choose 'Windows Authentication' or enter SQL 'username/password'.
- Select the database you want to connect to from the dropdown.

4. Test and Save Connection


• Click 'Test Connection'.
• If successful, click 'OK' to add the connection.

5. Use Connection in Code


Use the following C# code to establish a connection:

using System;
using System.Data.SqlClient;

class Program
{
static void Main()
{
string connectionString = "Server=YOUR_SERVER_NAME;
Database=YOUR_DATABASE_NAME; Integrated Security=True;";
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
con.Open();
Console.WriteLine("Connection Successful!");
}
catch (Exception ex)
{
Console.WriteLine("Connection Failed: " + ex.Message);
}
}
}
}

Below is an example image of the SQL Server connection setup:

You might also like