5/11/2016 C# SQL Server Connection
C# SQL Server Connection
You can connect your C# application to data in a
SQL Server database using the .NET Framework
Data Provider for SQL Server. The first step in a C#
application is to create an instance of the Server object and to establish its
connection to an instance of Microsoft SQL Server.
The SqlConnection Object is Handling the part of physical communication
between the C# application and the SQL Server Database . An instance of the
SqlConnection class in C# is supported the Data Provider for SQL Server
Database. The SqlConnection instance takes Connection String as argument and
pass the value to the Constructor statement.
Sql Server connection string
connetionString="Data Source=ServerName;
Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
If you have a named instance of SQL Server, you'll need to add that as well.
"Server=localhost\sqlexpress"
When the connection is established , SQL Commands will execute with the help
of the Connection Object and retrieve or manipulate the data in the database.
Once the Database activities is over , Connection should be closed and release
the Data Source resources .
[Link]();
The Close() method in SqlConnection Class is used to close the Database
Connection. The Close method rolls back any pending transactions and releases
the Connection from the SQL Server Database.
A Sample C# Program that connect SQL Server using connection string.
using System;
using [Link];
using [Link];
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
[Link] 1/4
5/11/2016 C# SQL Server Connection
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection cnn ;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Us
cnn = new SqlConnection(connetionString);
try
{
[Link]();
[Link] ("Connection Open ! ");
[Link]();
}
catch (Exception ex)
{
[Link]("Can not open connection ! ");
}
}
}
}
Connect via an IP address
connetionString="Data Source=IP_ADDRESS,PORT;
Network Library=DBMSSOCN;Initial Catalog=DatabaseName;
User ID=UserName;Password=Password"
1433 is the default port for SQL Server.
Trusted Connection from a CE device
connetionString="Data Source=ServerName;
Initial Catalog=DatabaseName;Integrated Security=SSPI;
User ID=myDomain\UserName;Password=Password;
This will only work on a CE device
Connecting to SQL Server using windows authentication
"Server= localhost; Database= employeedetails;
Integrated Security=SSPI;"
A sample c# program that demonstrate how to execute sql statement and read
data from SQL server.
using System;
using [Link];
using [Link];
namespace WindowsApplication1
[Link] 2/4
5/11/2016 C# SQL Server Connection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection connection ;
SqlCommand command ;
string sql = null;
SqlDataReader dataReader ;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserNa
sql = "Your SQL Statement Here , like Select * from product";
connection = new SqlConnection(connetionString);
try
{
[Link]();
command = new SqlCommand(sql, connection);
dataReader = [Link]();
while ([Link]())
{
[Link]([Link](0) + " ‐ " + [Link](1) + " ‐
}
[Link]();
[Link]();
[Link]();
}
catch (Exception ex)
{
[Link]("Can not open connection ! ");
}
}
}
}
A sample C# program that perform Data Manipulation tasks like Insert , Update ,
Delete etc. also perform by the ExecuteNonQuery() of SqlCommand Object.
Next : C# OLEDB Connection
Download Source Print Source Code
Code
using System;
using [Link];
using [Link];
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
[Link] 3/4
5/11/2016 C# SQL Server Connection
string connetionString = null;
SqlConnection connection ;
SqlCommand command ;
string sql = null;
connetionString = "Data Source=ServerName;Initial Catalog=Databa
sql = "Your SQL Statemnt Here";
connection = new SqlConnection(connetionString);
try
{
[Link]();
command = new SqlCommand(sql, connection);
[Link]();
[Link]();
[Link]();
[Link] (" ExecuteNonQuery in SqlCommand executed !!
}
catch (Exception ex)
{
[Link]("Can not open connection ! ");
}
}
}
}
[Link] 4/4