ITPub博客

首页 > Linux操作系统 > Linux操作系统 > mysql的正则替换方式

mysql的正则替换方式

原创 Linux操作系统 作者:dimen007 时间:2010-07-21 21:37:26 0 删除 编辑

在为外来版本翻译的时候,遇到这样的问题。要对去除括号内所有字符,替换掉,但是mysql无法简单的实现;找了下c++也不行,只能用更简单的c#了,正则替换好用,对于正则替换还真不是很懂,linux还可以。c#就只能拿来主义了。有空研究研究下.

cmysql.cs

    using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication6
{
    class cmysql
    {
        public string c1;
        public int c2;
        public string c3;
        public int c4;
        public string c5;
        public string c6;
    }
}

main.cs

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.Text.RegularExpressions;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {

            string sqlstr = "SELECT * FROM generalchestlistinfo";
            MySqlConnection DBConn = new MySqlConnection("server=127.0.0.1;User Id=test0;password=test;Database=koa");
            DBConn.Open();
            MySqlCommand DBComm = new MySqlCommand(sqlstr, DBConn);
            //MySqlDataReader DBReader = DBComm.ExecuteReader(); //DBComm.ExecuteReaderEx();
            MySqlDataAdapter DTAdapter = new MySqlDataAdapter(sqlstr, DBConn);

            DataSet myDataSet = new DataSet();
            DTAdapter.Fill(myDataSet, "tname");
           
            List lc = new List();
            Regex r = new Regex(@"\([^\)]*\)");
            //Regex r = new Regex(@"\(*\)");
            //Console.WriteLine(r.Replace("1(abc)2(2d4)3(12)" ,""));

            for (int i = 0; i < myDataSet.Tables[0].Rows.Count; i++)
            {
                cmysql cmy=new cmysql();
                cmy.c1 = myDataSet.Tables[0].Rows[i]["generalname"].ToString();
                //cmy.c3 = myDataSet.Tables[0].Rows[i]["Name"].ToString();
                cmy.c2 = Convert.ToInt32(myDataSet.Tables[0].Rows[i]["id"].ToString());
                //cmy.c4 = Convert.ToInt32(myDataSet.Tables[0].Rows[i]["level"].ToString());
                //cmy.c1=cmy.c1.Replace("地","天");
                lc.Add(cmy) ;
               
            }
            for(int i=0;i            {
                cmysql aa = lc[i];
                MySqlCommand mcommand = new MySqlCommand("UPDATE generalchestlistinfo SET `generalname` = '" + r.Replace(aa.c1, "") + "' WHERE id = " + aa.c2 , DBConn);
                mcommand.ExecuteNonQuery();
            }
            DBConn.Close();
            Console.WriteLine("finished");
            Console.ReadLine();
        }
     
       
    }
}

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12309491/viewspace-668653/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-06-17

  • 博文量
    11
  • 访问量
    20952