博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vs2008下 连接MySQL实例
阅读量:5079 次
发布时间:2019-06-12

本文共 2603 字,大约阅读时间需要 8 分钟。

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;
        }
     }
}

转载于:https://www.cnblogs.com/shihao/archive/2012/05/29/2524897.html

你可能感兴趣的文章
getElement的几中属性介绍
查看>>
HTML列表,表格与媒体元素
查看>>
设计器 和后台代码的转换 快捷键
查看>>
STL容器之vector
查看>>
数据中心虚拟化技术
查看>>
复习文件操作
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>
oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec
查看>>
python3 生成器与迭代器
查看>>
java编写提升性能的代码
查看>>
Abstract Factory Pattern
查看>>
list 容器 排序函数.xml
查看>>
《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇03:暂停游戏》
查看>>
CPU,寄存器,一缓二缓.... RAM ROM 外部存储器等简介
查看>>
windows下编译FreeSwitch
查看>>
git .gitignore 文件不起作用
查看>>