/
Database.cs
137 lines (126 loc) · 4.15 KB
/
Database.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//新版本的DBHelper
public abstract class Database
{
private string connString = "";
protected string ConnectionString
{
get
{
return connString;
}
}
public Database(string connectionString)
{
connString = connectionString;
}
protected abstract DbConnection CreateConnection();
public abstract DbDataAdapter CreateDataAdapter();
/// <summary>
/// 注意一定要写在using代码块中,因为我直接就open了一个连接
/// </summary>
/// <returns></returns>
public DbConnection GetConnection()
{
DbConnection connection = CreateConnection();
connection.Open();
return connection;
}
public DbCommand GetCommand(DbConnection connection, CommandType commandType)
{
DbCommand command = connection.CreateCommand();
command.CommandType = commandType;
return command;
}
public DbCommand GetSqlCommand(DbConnection connection, string sql)
{
DbCommand command = connection.CreateCommand();
command.CommandText = sql;
command.CommandType = CommandType.Text;
return command;
}
public DbCommand GetStoredProcCommand(DbConnection connection, string storedProcedureName)
{
DbCommand command = connection.CreateCommand();
command.CommandText = storedProcedureName;
command.CommandType = CommandType.StoredProcedure;
return command;
}
public void AddParameter(DbCommand command, string name)
{
DbParameter param = command.CreateParameter();
param.ParameterName = name;
command.Parameters.Add(param);
}
public void AddParameter(DbCommand command, string name, DbType dbType)
{
DbParameter param = command.CreateParameter();
param.ParameterName = name;
param.DbType = dbType;
command.Parameters.Add(param);
}
public void AddParameter(DbCommand command, string name, object value)
{
DbParameter param = command.CreateParameter();
param.ParameterName = name;
param.Value = value ?? (object)DBNull.Value;
command.Parameters.Add(param);
}
public void AddParameter(DbCommand command, string name, DbType dbType, object value)
{
DbParameter param = command.CreateParameter();
param.ParameterName = name;
param.DbType = dbType;
param.Value = value ?? (object)DBNull.Value;
command.Parameters.Add(param);
}
public DataTable ExecuteTable(DbCommand cmd)
{
using (DataSet st = new DataSet())
{
DbDataAdapter ap = CreateDataAdapter();
ap.SelectCommand = cmd;
ap.Fill(st);
return st.Tables[0];
}
}
public DataSet ExecuteSet(DbCommand cmd)
{
using (DataSet st = new DataSet())
{
DbDataAdapter ap = CreateDataAdapter();
ap.SelectCommand = cmd;
ap.Fill(st);
return st;
}
}
}
public class MYSQLDataBase : Database
{
public MYSQLDataBase(string conn)
: base(conn)
{
}
protected override DbConnection CreateConnection()
{
return new MySqlConnection(base.ConnectionString);
}
public override DbDataAdapter CreateDataAdapter()
{
return new MySqlDataAdapter();
}
}
public class MSSQLDataBase : Database
{
public MSSQLDataBase(string conn)
: base(conn)
{
}
protected override DbConnection CreateConnection()
{
return new MySqlConnection(base.ConnectionString);
}
public override DbDataAdapter CreateDataAdapter()
{
return new MySqlDataAdapter();
}
}