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方法即可插入数据了。
相关文档:
require() 与 require_once()
通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require
所指定引入的文件,如果出现错误是致命的。
nclude() 与 include_once()
&n ......
1.SET GLOBAL event_scheduler = ON;
2.show processlist;
3.创建一个表 test 主要字段 no,name,sex,age
4.对该表插如几条数据
5.创建存储过程add_age
drop procedure if exists add_age;
delimiter|
create procedure add_age()
begin
start transaction;
update eventtest set age = age +1;
&n ......
截取province字符串中第一个<br>前的字符串~!
update lcjd
set `province` = substring_index( `province` , '<br>', '1' );
在需要添加‘0’的位置添加一个‘0’
update lcjd
set lc_name2 = concat('0', lc_name2)
WHERE length(lc_name2) = 3
http://www.sqlstudy.com/s ......
protected void BindData1()
{
OracleConnection orcn = new OracleConnection("User ID=wesoftwcp; Password=wesoft; Data Source=oradb");
orcn.Open();
&nb ......
//主键
alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
//增加一个新列
alter table t2 add d timestamp;
alter table infos add ex tinyint not null default '0';
//删除列
alter table t2 drop column c;
//重命名列
......