1.引用文件
下载dll文件,添加引用
http://cdn.mysql.com//Downloads/Connector-Net/mysql-connector-net-6.9.9-noinstall.zip
程序开头加
using MySql.Data; using MySql.Data.MySqlClient;
2.设置数据库连接
http://zhidao.baidu.com/link?url=8N5VyQAsD2nRT7JpNrg2ebknKFxFRCuLX26V6xcfzCDtcdzPZ8kZb07qrEMOrVDHv5GB1K2pRSPHdNqisvjXcq
MySqlConnection mycon = new MySqlConnection("server=localhost;user id=root;password=root123;database=bizmap;Charset=utf8;");
查数据
mycon.Open(); MySqlCommand mycmd = new MySqlCommand("select * from versioninfo where versionName='" + versionName + "'", mycon); MySqlDataReader read = mycmd.ExecuteReader();
while(read.Read())
{
//...
} mycon.Close();
插入数据
MySqlConnection mycon = new MySqlConnection("server=localhost;user id=root;password=root123;database=bizmap;Charset=utf8;"); mycon.Open();
mycon.Open(); String sqlinsert1 = "insert into versioninfo values('" + versionName + "','" + updateDate + "','" + description + "')";
MySqlCommand mycmd = new MySqlCommand(sqlinsert1,mycon);
//mycmd.CommandText = sqlinsert1;
mycmd.ExecuteNonQuery();
mycon.Close();
3.数据库事务
http://www.360doc.cn/article/19147_265252035.html
SqlTransaction transaction = null; //数据库OPEN transaction = cnn.BeginTransaction(); cmd.Transaction = transaction; //数据库操作 transaction.Commit(); //数据库CLOSE
4.时间复杂度优化
- 数据库的OPEN,CLOSE操作往往占用大量时间,可以使用stringbuilder的append方法,拼接SQL语句,最终一并执行
- 字符串中使用startwith比使用contains复杂度要低,尽量使用前者
- 尽量减少循环层数,和数据库操作次数