
L p trình c s d li uậ ơ ở ữ ệ
I. Đ i t ng Sqlconnectionố ượ
Không gian tên s d ng là : System.Data.SqlClient ;ử ụ
1. K t n i theo đ c quy n h đi u hànhế ố ặ ề ệ ề
Cú pháp 1 :
Server = servername [ \InstanceName ];
Database = databasename;
Integrated Security = SSPI;
Cú pháp 1 :
Server = servername[ \InstanceName ];
Database = databasename;
Integrated Security = true;
Cú pháp 3 :
Server = servername [ \InstanceName ];
Initial Catolog = databasename;
Integrated Security = true;
•Chú ý : Tài kho n đăng nh p ph i đ c khai báo trong ph n Loginả ậ ả ượ ầ
2. K t n i theo đ c quy n SQL Serverế ố ặ ề
Cú pháp 1 (Chung) :
Server = Servername[ \InstanceName ];
Database = Databasename;
Use ID = Username;
Password = YourPasword;
[Connection Timeout = second;]
[port = portno;]
[Persist Security Info = true;]
•Chú ý : Servername có th là : (local) ho c ( . ) ho c (Đ a ch IP) .ể ặ ặ ị ỉ
Cú pháp 2 : V i d ng Attachment và phiên b n SQL Server 2005ớ ạ ả
(SQLEXPRESS)
@”Data Source = (local)\SQLEXPRESS;
AttachDbFilename = <Đ ng d n t i file Database>;ườ ẫ ớ
Integrated Security = true; Use Instance = true”;
3. T p tin l u chu i k t n iậ ư ỗ ế ố
Ta có th s d ng các đ nh d ng *.ini ho c *.txt đ l chu i k t n i .ể ử ụ ị ạ ặ ể ư ỗ ế ố
Tuy nhiên khi làm vi c v i .Net chúng ta nên s d ng đ nh d ng *.configệ ớ ử ụ ị ạ
đã đ c h tr s n.ượ ố ợ ẵ
- Cú pháp 1:
?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="SqlServer"
connectionString=
" Server = servername[ \InstanceName ];
Database = databasename;
Integrated Security = true;
"
providerName ="System.Data.SqlClient"/>

</connectionStrings>
</configuration>
Cách đ c n i dung chu i k t n i theo cú pháp 1 ta s d ng ph ng th cọ ộ ỗ ế ố ử ụ ươ ứ
connectionStrings c a l p connectionStringSettings thu c không gian tênủ ớ ộ
System.Configuration;
II. Đ i t ng SQLCommandố ượ
2.1 Khai báo
SqlCommand sqlCommand;
2.2 Kh i t oở ạ
Có 4 Constructor đ khai báo kh i t o đ i t ng nàyể ở ạ ố ượ
+ sqlCommand = new SqlCommand();
+ sqlCommand = new SqlCommand(string CommandText);
+ sqlCommand = new SqlCommand(string CommandText,SqlConnection
sqlConnection);
+ sqlCommand = new SqlCommand(string CommandText,SqlConnection
sqlConnection,SqlTrasaction sqlTransaction);
2.3 Các thu c tínhộ
2.3.1 CommandText
Cho phép khai báo m t chu i phát bi u SQL Serverộ ỗ ể
VD :
String strSQL = “Select * from TBLSinhvien”;
sqlCommand = new SqlCommand();
sqlCommand.CommandText = strSQL;
2.3.2 CommandType
Cho phép ta ch n m t trong 3 giá tr enum là :ọ ộ ị
Text,TableDirect,StoredProcedure
VD:
String strSQL = “spDanhsachSV”;
sqlCommand = new SqlCommand();
sqlCommand.CommandText = strSQL;am
sqlCommand.CommandType = CommandType.StoredProcedure;
•L u ý khi th t c có tham s truy n vào thì ta có th s d ng đ iư ủ ụ ố ề ể ử ụ ố
t ng SqlParameterCollection hay SqlParameterượ
2.3.3 CommandTimeout
Cho phép khai báo th i gian ch th c thi phát bi u SQL đ c tính b ngờ ờ ự ể ượ ằ
giây
VD :
String strSQL = “spDanhsachSV”;
sqlCommand = new SqlCommand();
sqlCommand.CommandText = strSQL;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandTimeout = 30;
2.3.4 Connection
Cho phép ta kh i t o đ i t ng sqlConnection mà không c n ph i thôngở ạ ố ượ ầ ả
qua Constructor
VD :
String strSQL = “spDanhsachSV”;
sqlCommand = new SqlCommand();

sqlCommand.CommandText = strSQL;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandTimeout = 30;
2.4 Ph ng th cươ ứ
2.4.1 Ph ng th c ExecuteNonQueryươ ứ
Th c thi các phát bi u SQL, th t c n i t i, và nó tr v s b n ghi đ oc th cự ể ử ụ ộ ạ ả ề ố ả ự ự
thi
VD :
sqlCommand.Connection = sqlConnection;
sqlConnnection.Open();
String strSQL = “delete from TBLSinhvien where Masv = ‘SV0000001’”;
sqlCommand = new SqlCommand();
sqlCommand.CommandText = strSQL;
int records = sqlCommand.ExcuteNonQuery();
sqlConnection.Close();
sqlConnection.Dispose();
Th c hi n thêm , s a , xoá m t b n ghi :ự ệ ử ộ ả
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace DoituongSQLCommand
{
public partial class Form1 : Form
{
// Khai bao doi tuong sqlConnection
SqlConnection sqlConnection = new SqlConnection();
string connectionString = @"server
=QUYETNV87\SQLEXPRESS ;" + "database = Sinhvien ;" +
"Integrated Security = True;";
public Form1()
{
InitializeComponent();
}
private void btnSQLinsert_Click(object sender, EventArgs e)
{
try
{
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
String strSQL = "insert into
TBLSinhvien(MaSV,Hoten,Gioitinh,Ngaysinh,Malop,M
atinh) values('SV0000011','Tr n Qu cầ ố
Huy','Nam','07/19/1986','1900052','T09')";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText = strSQL;
sqlCommand.Connection = sqlConnection;
int i = sqlCommand.ExecuteNonQuery();
// Đóng k t n iế ố
sqlConnection.Close();
// Gi i phóng kêt n i c s d li uả ố ơ ở ữ ệ

sqlConnection.Dispose();
MessageBox.Show("Đã thêm " + i.ToString() + " b n ghi"ả);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnSQLupdate_Click(object sender, EventArgs e)
{
try
{
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
String strSQL = "Update TBLSinhvien set Matinh
='T15' where Matinh='T05'";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText = strSQL;
sqlCommand.Connection = sqlConnection;
int i = sqlCommand.ExecuteNonQuery();
// Đóng k t n iế ố
sqlConnection.Close();
// Gi i phóng kêt n i c s d li uả ố ơ ở ữ ệ
sqlConnection.Dispose();
MessageBox.Show("Đã c p nh t "ậ ậ + i.ToString() + " b n ả
ghi");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSQLdelete_Click(object sender, EventArgs e)
{
try
{
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
String strSQL = "Delete TBLSinhvien where
MaSV='SV0000011'";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText = strSQL;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandTimeout = 60;
int i = sqlCommand.ExecuteNonQuery();
// Đóng k t n iế ố
sqlConnection.Close();
// Gi i phóng kêt n i c s d li uả ố ơ ở ữ ệ
sqlConnection.Dispose();
MessageBox.Show(i.ToString() + " b n ghi đã đ c xoá "ả ượ );
}
catch (Exception ex)

{
MessageBox.Show(ex.Message);
}
}
private void btnInsertAttach_Click(object sender,EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
string connectionString = @"server=(local)\SQLEXPRESS ;"
+ @"AttachDbFilename
=E:\LAPTRINH\Database\Sinhvien.mdf;"
+ @"Integrated Security = True;"
+ @"User Instance= true;";
sqlConnection.ConnectionString = connectionString;
try
{
sqlConnection.Open();
String strSQL = "insert into
TBLSinhvien(MaSV,Hoten,Gioitinh,Ngaysinh,Malop,M
atinh) values('SV0000011','Tr n Qu cầ ố
Huy','Nam','07/19/1986','1900052','T09')";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText = strSQL;
sqlCommand.Connection = sqlConnection;
int i= sqlCommand.ExecuteNonQuery();
// Đóng k t n iế ố
sqlConnection.Close();
// Gi i phóng kêt n i c s d li uả ố ơ ở ữ ệ
sqlConnection.Dispose();
MessageBox.Show("");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
2.4.2 Ph ng th c ExecuteScalarươ ứ
Ph ng th c này th c thi phát bi u SQL Server giá tr tr v là ki u đ iươ ứ ự ể ị ả ề ể ố
t ng (object)ượ
Nó th ng đ c dùng đ l y giá tr c a t ng các m u tin hay giá tr c a c tườ ượ ể ấ ị ủ ổ ấ ị ủ ộ
hay hàng th nh tứ ấ
Ví d :ụ
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace ExecuteScalar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

