How to setup an SQL connection in .Net (C#)?

SQL connection setup Steps

Step1: Create a Database (Eg MyFirstDB)
Step2: Create a table in MyFirstDB (Eg Tbl_MyFirstTable)
Step3: Create a new project in Visual Studio.
Step4: Establish Connection using SQL Client (As Shown below)

Open the aspx.cs page (Eg: Default.aspx.cs)
Use the following code.

Using System.Data;
Using System.Data.SqlClient;
public partial class Home : System.Web.UI.Page
string conString = @”Data Source=SERVER1;Initial Catalog=MyFirstDB;User ID=sa;Password=password”;
SqlConnection objConnection;

protected void Page_Load(object sender, EventArgs e)

objConnection=new SqlConnection(conString);

protected void Button1_Click(object sender, EventArgs e)
Label1.Text = “Connection opened successfully”;
Label1.Text = “Connection closed successfully”;
Label1.Text = “Connection failed”;

Read More from MSDN


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s