C#连接MySQL进行操作的方法
由于需要实现以下功能:
网关通过串口发送数据给PC机,PC机收集数据并解析保存到MySQL中,然后JSP页面读取MySQL中的数据并显示。
所以利用C#连接MySQL数据成为了必须要经过的过程,在此给予详细的说明。
1、下载需要的文件MySQLDriverCS,下载地址为:http://sourceforge.net/projects/mysqldrivercs
2、安装文件:MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe,在安装过程中也许会提示一个错误,禁止写一个dll文件,你直接忽略就可以了。
3、将安装文件下的MySQLDriver.dll文件添加到C#项目的引用中。
4、然后写公共处理类如下:
(1) 首先得引用命名空间:
using MySQLDriverCS;
using System.Data;
using System.Data.Odbc;
(2) 写公共处理类
public class SQLHelper
{
MySQLConnection conn = null;
MySQLCommand commn = null;
public MySQLConnection GetConnect()
{
conn = new MySQLConnection(new MySQLConnectionString("localhost", "wwater", "root", "123").AsString);
return conn;
}
public int RunMySQL(string sql)
{
int count = 0;
try
{
conn = GetConnect();
conn.Open();
commn = new MySQLCommand(sql, conn);
count = commn.ExecuteNonQuery();
return count;
}
catch (Exception ex)
{
throw;
}
finally
{
conn.Close();
}
}
}
(3) 在实际过程中进行引用RunMySQL方法即可插入数据了。
相关文档:
地址: http://imysql.cn/taxonomy/term/1?page=1
[InnoDB系列] -- innodb表如何更快得到count(*)结果:http://imysql.cn/2008_06_24_speedup_innodb_count
[InnoDB系列系列] -- 大数据量的导出导入方法比较:
http://imysql.cn/2007_10_15_large_innodb_table_export_import
[MySQL优化案例]系列 -- DISABLE/ENABLE K ......
官方手册上是这么写的:
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{f ......
假设mysql 安装在c:盘,mysql数据库的用户名是root,密码是123456,数据库名是testdb,在d:盘根目录下面存放备份数据库,备份数据库名字为backup20070713.sql(20070713.sql为备份日期)
备份数据库:
mysqldump -uroot -p123456 testdb>d:/backup20070713.sql
恢复数据库:
删除原有数据库,建立数据库,把备份数据 ......
1.SELECT
*
from
table
LIMIT
1
,
20
;
//
检索记录行
2-21
第一个参数:查询起始行(从0开始)
第二个参数:查询几条记录
2.
SELECT
*
from
table
LIMIT
5
,
-
1
;
//
&nbs ......
#
-*- encoding: gb2312 -*-
import
os, sys, string
import
MySQLdb
#
连接数据库
try
:
conn
=
MySQLdb.connect(host
=
'
localhost
'
,user
=
'
root
'
,passwd
=
'
xxxx
'
,db
=
'
test1
'
)
except
Exception, e:
print
e
sys.exit()
......