In this post we are going to create a connection with Microsoft SQL Server using .Net Framework and we would be using C# as our language.
In order to perform SQL related tasks we need to use SqlClient under Data therefore we would add following line in the code window of each form where we will be performing SQL related tasks.
using System.Data.SqlClient; |
Next we need to create a connection which we will be using for dealing with SQL Server. There are many methods to do that but what we will be doing is we will create a static connection so that we can use that connection at all the places and any Server related changes can be done at only one place. Other option is to create a connection anywhere as and when required but it adds a problem when you need to modify the server properties.
For windows application we will be doing this in Program.cs file and for web application we will be doing this by creating a new class.
Here we are demonstrating the same for windows application.
In Program.cs file after writing following line at the top section
using System.Data.SqlClient; |
public static SqlConnection Conn = new SqlConnection("data source=localhost; integrated security=sspi;initial catalog=database_name"); |
data source=localhost;
integrated security=sspi;
initial catalog=database_name;
Durgesh bhai, you seem to be a geek in coding for C# , .Net and SQL .
Thanks Kundan for nice words.