vs2008下 连接MySQL实例
2012-05-29 23:21
211 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Data;
using MySQLDriverCS;
namespace SearchBiDui
{
class Program
{
static void Main(string[] args)
{
MySqlConnection Conn = new MySqlConnection("Database='dmoz';Data Source='localhost';User Id='root';Password='123456';charset=utf8");//连接MySQL数据库
MySqlDataAdapter Da = new MySqlDataAdapter("select * from 4500words ", Conn);//从数据表中取出数据,没有做下次不重复的工作
DataTable Dt = new DataTable();
Da.Fill(Dt);
int count = Dt.Rows.Count; //获取记录集的长度
for (int i = 0; i < count; i++)
{
int id = Convert.ToInt32(Dt.Rows[i]["id"].ToString());
string word = Dt.Rows[i]["word"].ToString();
string pinyin = Dt.Rows[i]["pinyin"].ToString();
string word01 = word + "_";
string word02 = "_" + word;
MySqlDataAdapter Da1 = new MySqlDataAdapter("SELECT word,pinyin FROM userword2 where word like '" + word01 + "' and one = '" + pinyin + "' union all select word,pinyin from userword2 where word like '" + word02 + "' and two = '" + pinyin + "' ", Conn);
DataTable Dt1 = new DataTable();
Da1.Fill(Dt1);
int count1 = Dt1.Rows.Count;
string speech = "";
int pos = 0;
for (int j = 0; j < count1; j++)
{
string word1 = Dt1.Rows[j]["word"].ToString();
string pinyin1 = Dt1.Rows[j]["pinyin"].ToString();
if (word1[0].ToString() == word)
{
pos = 1;
}
else if (word1[1].ToString() == word)
{
pos = 2;
}
else
{
pos = 0;
Console.WriteLine("读词有误!");
break;
break;
}
if (pos != 0)
{
speech = speech + word1 + "|" + pinyin1 + "$" + pos.ToString() + "#";
}
}
string sql = "update 4500words set speech ='" + speech + "' where id='" + id + "' ";
RunSqlDatacmd(sql);
if (i % 10 == 0)
{
Console.WriteLine("已经执行了" + i + "条");
}
}
}
public static long RunSqlDatacmd(string sql) //sql语句执行成员
{
MySqlConnection dbconn = new MySqlConnection("Database='dmoz';Data Source='localhost';User Id='root';Password='123456';charset=utf8");
MySqlCommand cmd = dbconn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
long ret = 0;
try
{
if (cmd.Connection.State == ConnectionState.Broken)
{
cmd.Connection.Close();
cmd.Connection.Open();
}
else if (cmd.Connection.State == ConnectionState.Closed)
{
cmd.Connection.Open();
}
// else if (cmd.Connection.State == ConnectionState.Open)
// {
ret = cmd.ExecuteNonQuery();
// }
//else
// {
// ret = -102;
// }
}
catch (Exception ex)
{
string m = ex.Message;
ret = -5;
}
cmd.Dispose();
dbconn.Close();
return ret;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Data;
using MySQLDriverCS;
namespace SearchBiDui
{
class Program
{
static void Main(string[] args)
{
MySqlConnection Conn = new MySqlConnection("Database='dmoz';Data Source='localhost';User Id='root';Password='123456';charset=utf8");//连接MySQL数据库
MySqlDataAdapter Da = new MySqlDataAdapter("select * from 4500words ", Conn);//从数据表中取出数据,没有做下次不重复的工作
DataTable Dt = new DataTable();
Da.Fill(Dt);
int count = Dt.Rows.Count; //获取记录集的长度
for (int i = 0; i < count; i++)
{
int id = Convert.ToInt32(Dt.Rows[i]["id"].ToString());
string word = Dt.Rows[i]["word"].ToString();
string pinyin = Dt.Rows[i]["pinyin"].ToString();
string word01 = word + "_";
string word02 = "_" + word;
MySqlDataAdapter Da1 = new MySqlDataAdapter("SELECT word,pinyin FROM userword2 where word like '" + word01 + "' and one = '" + pinyin + "' union all select word,pinyin from userword2 where word like '" + word02 + "' and two = '" + pinyin + "' ", Conn);
DataTable Dt1 = new DataTable();
Da1.Fill(Dt1);
int count1 = Dt1.Rows.Count;
string speech = "";
int pos = 0;
for (int j = 0; j < count1; j++)
{
string word1 = Dt1.Rows[j]["word"].ToString();
string pinyin1 = Dt1.Rows[j]["pinyin"].ToString();
if (word1[0].ToString() == word)
{
pos = 1;
}
else if (word1[1].ToString() == word)
{
pos = 2;
}
else
{
pos = 0;
Console.WriteLine("读词有误!");
break;
break;
}
if (pos != 0)
{
speech = speech + word1 + "|" + pinyin1 + "$" + pos.ToString() + "#";
}
}
string sql = "update 4500words set speech ='" + speech + "' where id='" + id + "' ";
RunSqlDatacmd(sql);
if (i % 10 == 0)
{
Console.WriteLine("已经执行了" + i + "条");
}
}
}
public static long RunSqlDatacmd(string sql) //sql语句执行成员
{
MySqlConnection dbconn = new MySqlConnection("Database='dmoz';Data Source='localhost';User Id='root';Password='123456';charset=utf8");
MySqlCommand cmd = dbconn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
long ret = 0;
try
{
if (cmd.Connection.State == ConnectionState.Broken)
{
cmd.Connection.Close();
cmd.Connection.Open();
}
else if (cmd.Connection.State == ConnectionState.Closed)
{
cmd.Connection.Open();
}
// else if (cmd.Connection.State == ConnectionState.Open)
// {
ret = cmd.ExecuteNonQuery();
// }
//else
// {
// ret = -102;
// }
}
catch (Exception ex)
{
string m = ex.Message;
ret = -5;
}
cmd.Dispose();
dbconn.Close();
return ret;
}
}
}
相关文章推荐
- 解耦合 jsp 连接 mysql 的实例代码 jsp连接数据库的最好方法
- MySQL中的连接、实例、会话、数据库、线程之间的关系
- C#连接操作 MySQL 数据库实例(使用官方驱动)
- ASP.NET连接mysql 数据库实例
- mysql设置指定ip远程访问连接实例
- java程序中的ibatis连接mySql的基本实例
- MySQL高级查询---连接查询实例
- vs2008下MFC中采用ado连接MySQL(ODBC、非ODBC或C API方式)
- 连接mysql的javabean实例+简单分页
- MySQL连接实例
- MYSQL复杂多表内外连接实例
- Mybatis连接MySQL或者ORALE 12C 入门实例。
- JDBC连接数据库实例(mysql)
- C#连接mysql实例
- python 26 连接 mysql 实例
- [转]C#连接操作mysql实例
- PHP 连接mysql 实例代码
- RedHat 上安装多个 mysql 实例并配置 django 连接的操作记录
- MySQL多实例的环境下,服务器端本地连接到指定实例的问题(sock方式连接)
- MySql和VS2008连接遇到的问题