从sqlserver到mysql的数据导入导出

private string ImportData(string TableName)
        {
            //try
            //{
                //string mystrconn = "server=192.168.1.228;database=db_music;uid=a8user;pwd=a8user;";
                //MySql.Data.MySqlClient.MySqlConnection myconn = new MySqlConnection(mystrconn);
                //myconn.Open();

                Application.DoEvents();

                string sql = string.Format("select {1} * from {0}", TableName, this.txtTop.Text == "" ? "" : "top " + this.txtTop.Text);
                DataSet ds = SqlHelper.ExecuteDataset(txtSqlConn.Text, CommandType.Text, sql);
               
                int curPage = 0;
                int countSize = ds.Tables[0].Rows.Count;
                if (this.txtTop.Text != "" && countSize>int.Parse(this.txtTop.Text))
                    countSize = int.Parse(this.txtTop.Text);
                int pagesize = countSize;
                if (this.textBox2.Text != "")
                    pagesize = int.Parse(this.textBox2.Text);
                string filepath = this.textBox1.Text + TableName + curPage.ToString() + ".txt";
                FileStream fs = File.Create(filepath);
                string strGenSql = "";
                for (int i = 0; i < countSize; i++)
                {
                    Application.DoEvents();
                    if ((i % pagesize) == 0)
                    {
                        curPage++;
                        filepath = this.textBox1.Text + TableName + curPage.ToString() + ".txt";
                        fs = File.Create(filepath);
                    }
                    string strValue = "";
                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                    {
                        strValue += GetFormatValue(ds.Tables[0].Rows[i][j].ToString(), ds.Tables[0].Columns[j].DataType.ToString()) + ",";
                    }
                    if (strValue != "") strValue = strValue.Substring(0, strValue.Length - 1);
                    string exsql = string.Format("insert into {0} select {1};/r/n", TableName, strValue);
                    //strGenSql += exsql;
                    Byte[] info = System.Text.Encoding.Default.GetBytes(exsql);
//注意字符集的选择,不合适的字符集只会导致乱码
                    //Byte[] info = new UTF8Encoding(true).GetBytes(exsql);
                    fs.Write(info, 0, info.Length);
                    this.progressBar2.Value = ((i + 1) / countSize) * this.progressBar2.Maximum;
                    Application.DoEvents();
                }
                fs.Close();
               
                return strGenSql;
            //}
            //catch (Exception)
            //{
               
                //throw;
            //}
           
           
        }

 

 private string GetFormatValue(string fdValue, string fdType)
        {
            if (fdValue == "")
                return GetNullValue(fdValue);
//“/”在mysql里是转意符,故要小心处理,“/n”在导入时可能产生乱码
            if (fdType.IndexOf("Int") > -1)
                return fdValue.Replace("'", "//'").Replace("/n", "/r/n");
            else if (fdType.IndexOf("DateTime") > -1)
                return "'" + fdValue.Replace("'", "//'").Replace("/n", "/r/n") + "'";
            else
                return "'" + fdValue.Replace("//", "").Replace("//'").Replace("/n", "/r/n").Replace("&quot;", "/"") + " ' "; //最后加空格是为了避免数据源里乱码的干扰,当完全导入后记得trim()一次
        }

        private string GetNullValue(string fdType)
        {
            switch (fdType)
            {
                case "int":
                    return "null";
                    break;
                case "varchar":
                    return "''";
                    break;
                default:
                    return "null";
                    break;
            }
        }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值