ADO.NET - ADO.NET SQL Server
ADO.NET SQL Server Connection
Để kết nối với SQL Server, chúng ta phải cài đặt SQL Server vào hệ thống. Chúng ta sử dụng SQL Server Management Tool để kết nối với SQL Server. Sau đây là các bước kết nối với SQL Server
-
Mở Microsoft SQL Server Management Tool
Chọn CSDL và quyền login.
Sau khi kết nối thành công, sẽ xuất hiện cửa sồ sau:
2. Tạo CSDL
Bây giờ, tạo CSDL bằng cách chọn vào mục database click chuột phải, xuất hiện cửa sổ menu cho new database
Click vào New Database sau đó nhập vào tên database . Chúng ta tạo một CSDL Student.
Click vào Ok nó sẽ tạo một CSDL có tên Student. Chúng ta nhìn cửa sổ bên trái của màn hình.
3. Thiết lập kết nối và tạo bảng.
Sau khi tạo xong CSDL, bây giơ, chúng ta tạo bảng bằng code C#. Trong code C# chúng ta kết nối với CSDL student.
After creating database, now, let's create a table by using the following C# code. In this source code, we are using created student database to connect.
Dùng visual studio 2017, Chúng ta tạo một ứng dụng console .NET với code C#.
- Mở Microsoft SQL Server Management Tool
- Tạo CSDL
- Thiết lập kết nối và tạo bảng
// Program.cs
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class Program
{
static void Main(string[] args)
{
new Program().CreateTable();
}
public void CreateTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("create table student(id int not null,
name varchar(100), email varchar(50), join_date date)", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Table created Successfully");
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong."+e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
Ctrl+F5. Sẽ xuất kết quả sau.
Chúng ta có thể thấy bảng vừa được tạo trong Microsoft SQL Server Management Studio.
Như đã thấy bảng vừa tạo chưa có dữ liệu
4. Thêm dữ liệu vào bảng
// Program.cs
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class Program
{
static void Main(string[] args)
{
new Program().CreateTable();
}
public void CreateTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("insert into student
(id, name, email, join_date)values('101','Ronald Trump','ronald@example.com','1/12/2017')", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Record Inserted Successfully");
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong."+e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
Ctrl+F5 và xuất hiện kết quả sau.

5. Đọc dữ liệu
Code C# đọc dữ liệu từ CSDL.
// Program.cs
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class Program
{
static void Main(string[] args)
{
new Program().CreateTable();
}
public void CreateTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("Select * from student", con);
// Opening Connection
con.Open();
// Executing the SQL query
SqlDataReader sdr = cm.ExecuteReader();
// Iterating Data
while (sdr.Read())
{
Console.WriteLine(sdr["id"] + " " + sdr["name"]+" "+sdr["email"]); // Displaying Record
}
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n"+e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
Ctrl+F5 và xuất hiện 2hai sinh viên sau.
6. Xóa mẫu tin
Hiện tại tronng bảng student có hai mẫu tin. Code C# sau xóa 1 dòng trong bảng.
// Program.cs
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class Program
{
static void Main(string[] args)
{
new Program().CreateTable();
}
public void CreateTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("delete from student where id = '101'", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
Console.WriteLine("Record Deleted Successfully");
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n"+e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
Kết quả: