forked from rousseauuu/LibraryManageSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dao.cs
40 lines (39 loc) · 1.1 KB
/
Dao.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Data.SqlClient;
namespace BookManagementSystem
{
internal class Dao
{
SqlConnection sc;
// 连接数据库
public SqlConnection Connect()
{
// 数据库连接字符串 DESKTOP-A0467QR DESKTOP-N1AQVOP
string str = @"Data Source=DESKTOP-A0467QR;Initial Catalog=BookDB;Integrated Security=True";
// 创建数据库连接对象
sc = new SqlConnection(str);
sc.Open();
return sc;
}
// 生成对数据库的操作对象
public SqlCommand Command(string sql)
{
SqlCommand cmd = new SqlCommand(sql, Connect());
return cmd;
}
// 更新操作, 返回受影响的行数
public int Execute(string sql)
{
return Command(sql).ExecuteNonQuery();
}
// 读取操作
public SqlDataReader Read(string sql)
{
return Command(sql).ExecuteReader();
}
// 关闭数据库连接
public void DaoClose()
{
sc.Close();
}
}
}