展开全部
(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)封装数据库访e69da5e887aa3231313335323631343130323136353331333361316638问组件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
///
/// MySQL数据库
/// 版本 mysql-connector-net-6.3.8.msi
///
[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 = 0; j < dt.Columns.Count; j++)
{
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=localhost;User Id=root;password=root;Database=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("删除语句成功!下面是SQL语句" + 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("插入语句成功!下面是SQL语句"+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;
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起