-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBLink.cs
59 lines (54 loc) · 1.47 KB
/
DBLink.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Data;
using System.Data.SqlClient;
namespace Co_Manage_Sys
{
/// <summary>
/// 此类维护数据库连接字符串,和 Connection 对象
/// </summary>
public class DBLink
{
// 数据库连接字符串
private string connString = @"Data Source=ASUNA\SQLEXPRESS;Initial Catalog=SCT_WH_DataBase;Integrated Security=True";
// 数据库连接 Connection 对象
private SqlConnection connection;
/// <summary>
/// Connection对象
/// </summary>
public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
return connection;
}
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void CloseConnection()
{
if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
{
Connection.Close();
}
}
}
}