public static class database
{
//数据库链接
static MySqlConnection Conn = null
//连接数据库
static void OpenConn()
{
if (Conn != null &&Conn.State == ConnectionState.Open)
{
Conn.Close()
}
if (Conn == null)
{
Conn = new MySqlConnection(你的数据库连接字符串)
}
if (Conn.State == ConnectionState.Closed)
{
Conn.Open()
}
}
//关闭数据库连接
static void CloseConn()
{
if (Conn != null)
{
Conn.Close()
}
}
//执行SQL语句
public static bool ExecSql(string strSql)
{
try
{
OpenConn()
MySqlCommand Cmd = new MySqlCommand(strSql, Conn)
Cmd.ExecuteNonQuery()
Cmd.Dispose()
return true
}
catch (Exception ex)
{
return false
}
finally
{
CloseConn()
}
}
//获取DataSet接口
public static DataSet GetDataSet(string strSql, string strDataSetName)
{
try
{
OpenConn()
DataSet dataSet = new DataSet()
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(strSql, Conn)
dataAdapter.Fill(dataSet, strDataSetName)
dataAdapter.Dispose()
return dataSet
}
catch(Exception ex)
{
return null
}
finally
{
CloseConn()
}
}
//获取MySqlDataReader接口
public static MySqlDataReader GetDataReader(string strSql)
{
try
{
OpenConn()
MySqlCommand Cmd = new MySqlCommand(strSql, Conn)
MySqlDataReader dataReader = Cmd.ExecuteReader()
return dataReader
}
catch (Exception ex)
{
return null
}
}
}
要去网上下一个MySql.Data.dll 然后倒入就可以使用上面的代码连接数据库了
(1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序mysql-connector-net-6.3.8.msi
(2)安装mysql-connector-net
然后直接在Windows *** 作系统安装 mysql-connector-net-6.3.8.msi
(3)封装数据库访问组件DbConnectionMySQL
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
/// <summary>
/// MySQL数据库
/// 版本 mysql-connector-net-6.3.8.msi
/// </summary>
[Serializable]
public class DbConnectionMySQL : DbConnectionWrapper
{
public DbConnectionMySQL(string pConnectionString)
: base(pConnectionString)
{
this.m_dbconn = new MySqlConnection(pConnectionString)
this.m_DbConnState = DbConnState.Free
}
//--
public override DbDataAdapter GetDbDataAdapter()
{
return new MySqlDataAdapter()
}
public override DbDataAdapter GetDbDataAdapter(DbCommand dbCommand)
{
return new MySqlDataAdapter(dbCommand as MySqlCommand)
}
public override DbCommand GetDbCommand()
{
return new MySqlCommand()
}
public override DbConnection GetDbConnection()
{
return new MySqlConnection()
}
public override DbCommandBuilder GetDbCommandBuilder()
{
return new MySqlCommandBuilder()
}
public override DataProviderType GetCurrentDataProviderType()
{
return DataProviderType.Sql
}
public override bool IsExistsTable(string TableName, string UserName)
{
#region information
bool rbc = false //TABLES表中去查询 table_name
string dSql = "select * from TABLES where table_name='" + TableName + "'"
DataSet ds = this.ExecuteDataSet(dSql)
if (ds != null)
{
if (ds.Tables[0].Rows.Count >0)
{
rbc = true
}
else
{
rbc = false
}
}
else
{
rbc = false
}
return rbc
#endregion
}
public override bool IsExistsField(string FieldName, string TableName)
{
#region information
bool rbc = false
string dSql = ""
dSql = "select * from " + TableName + " where 1<>1"
DataSet ds = this.ExecuteDataSet(dSql)
if (ds != null)
{
DataTable dt = ds.Tables[0]
for (int j = 0j <dt.Columns.Countj++)
{
if (dt.Columns[j].ColumnName.ToString().ToUpper() == FieldName.ToString().ToUpper())
{
rbc = true
goto Return_End
}
}
dt.Dispose()
dt = null
}
ds.Dispose()
ds = null
Return_End:
return rbc
#endregion
}
public override char ParameterChar
{
get
{
return ':' //SQLite的参数符号为:
}
}
public override DbParameter CreateParameter(string name, object value)
{
return new MySqlParameter(name, value)
}
public override DbParameter CreateParameter(string name)
{
DbParameter dbp = new MySqlParameter()
dbp.ParameterName = name
return dbp
}
public override DbParameter CreateParameter(string name, DbType dbtype, object value)
{
DbParameter dbp = new MySqlParameter()
dbp.ParameterName = name
dbp.Value = value
dbp.DbType = dbtype
return dbp
}
public override DbParameter CreateParameter(string name, DbType dbtype, int size, object value)
{
DbParameter dbp = new MySqlParameter()
dbp.ParameterName = name
dbp.Value = value
dbp.DbType = dbtype
dbp.Size = size
return dbp
}
}
(4)客户端开发实例
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
public void TestCShape_MySQL()
{
string constr = "server=localhostUser Id=rootpassword=rootDatabase=xp_users"
DbConnectionWrapper dbw = new DbConnectionMySQL(constr)
bool rbc=dbw.TestConnection()
this.Context.Response.Write(rbc)
string x = ""
//删除语句
x = "delete from xp_users"
if (dbw.ExecuteQuery(x) >0)
{
this.Context.Response.Write("删除语句成功!下面是<a href="https://www.baidu.com/s?wd=SQL%E8%AF%AD%E5%8F%A5&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBnvD3Pjn3uWbvnj9WmyNW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHmsPHndP101n1nLnjDYPHD3Ps" target="_blank" class="baidu-highlight">SQL语句</a>" + x)
}
//插入语句
x = "insert into xp_users(gid,uid,uname,sex,email,pwd) values('"
x += "1','hsg77','何XXX',1,'hsg77@163.com','1')"
if (dbw.ExecuteQuery(x) >0)
{
this.Context.Response.Write("插入语句成功!下面是<a href="https://www.baidu.com/s?wd=SQL%E8%AF%AD%E5%8F%A5&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBnvD3Pjn3uWbvnj9WmyNW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHmsPHndP101n1nLnjDYPHD3Ps" target="_blank" class="baidu-highlight">SQL语句</a>"+x)
}
//查询语句
DataTable dt = dbw.ExecuteDataTable("select * from xp_users")
if (dt != null &&dt.Rows.Count >0)
{
this.Context.Response.Write("用户数:"+dt.Rows.Count)
}
if (dt != null)
{
dt.Dispose()
dt = null
}
dbw.Dispose()
dbw = null
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)