您现在的位置: 网页制作教程网 >> 数据库教程 >> 数据库技巧 >> 文章正文

使用程序导出建表语句,及以Insert语句形式导出数据

作者:动态网站…

来源:动态网站制作指南

热度:

2006-12-11 8:54:46

3)MySql:

获得用户表信息

show tables;

MySql中可以不必自己去拼建表语句,使用以下Sql获得建表语句

show create table tablename;

获取表的字段信息

show columns from tablename;

 

相关程序(省略部分代码):

 

private void ShowAllTable()
{
    ……
    this.cmd.CommandText = "show tables;";
    try
    {
        this.conn.Open();
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "alltable");
        this.Invoke((MethodInvoker)delegate
        {
            this.lboxAll.DataSource = ds.Tables["alltable"].DefaultView;
            this.lboxAll.DisplayMember = "Tables_in_"+this.tbDatabase.Text.Trim();
        });
    }
    catch (Exception x)
    {
        MessageBox.Show(x.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
    }
}


private void ExpSQL()
{
    this.sw = new StreamWriter(this.path, true, Encoding.Default);
    try
    {
        this.conn.Open();
        for (int i = 0; i < this.lboxSelected.Items.Count; i++)
        {
            this.cmd.CommandText = "show columns from " +
this.lboxSelected.Items[i].ToString();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "colinfo");
            DataTable dt = ds.Tables["colinfo"];

            this.cmd.CommandText = "select * from " + this.lboxSelected.Items[i].ToString();
            da = new MySqlDataAdapter(cmd);
            da.Fill(ds, "datainfo");
            DataTable dt2 = ds.Tables["datainfo"];

            this.GenerateCreateStatement(this.lboxSelected.Items[i].ToString());//
            this.GenerateInsertStatement(dt, dt2, this.lboxSelected.Items[i].ToString());

            Thread.Sleep(10);
        }
        this.sw.Flush();
        this.sw.Close();
        this.sw.Dispose();
        MessageBox.Show("完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception x)
    {
        MessageBox.Show(x.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
    }
}

private void GenerateCreateStatement(string tname)
{
    string sql = "";
    try
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
        this.cmd.CommandText ="show create table " + tname;
        MySqlDataReader dr = this.cmd.ExecuteReader();
        if (dr.Read())
        {
            sql = dr["Create Table"].ToString();
        }
    }
    catch (Exception x)
    {
        MessageBox.Show(x.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
    }

    sql = sql.Replace("`", "") + ";" + Environment.NewLine;
    this.sw.Write(sql);
    this.sw.Write(Environment.NewLine);
}

private void GenerateInsertStatement(DataTable dtColInfo, DataTable dtDataInfo,string tname)
{
    string sql =  "INSERT INTO " + tname + "(";

    string values = "";
    string colName = "";
    string dataType = "";
    for (int i = 0; i < dtDataInfo.Rows.Count; i++)
    {
        values = "VALUES(";
        for (int j = 0; j < dtColInfo.Rows.Count; j++)
        {
            colName = dtColInfo.Rows[j]["Field"].ToString();
            dataType = dtColInfo.Rows[j]["Type"].ToString();
            if (i == 0)
            {
                sql += colName + ",";
            }
            if (dtDataInfo.Rows[i][colName].ToString() == "")
            {
                values += "NULL,";
            }
            else
            {
                if (dataType.StartsWith("varchar") || dataType.StartsWith("char") ||
dataType == "datetime" || dataType == "date")
                {
                    values += "'" +
dtDataInfo.Rows[i][colName].ToString().Trim().Replace("\'", "\'\'") + "',";
                }
                else//
                {
                    values += dtDataInfo.Rows[i][colName].ToString() + ",";
                }
            }
        }
        this.sw.Write(sql.Substring(0, sql.LastIndexOf(',')) + ")" + Environment.NewLine +
values.Substring(0, values.LastIndexOf(',')) + ");" + Environment.NewLine);
        this.sw.Write(Environment.NewLine);
    }
}


程序里有好些方面是没有处理的,包括:表名、字段名为数据库保留字;表名、字段名含有特殊字符(如空格);一些数据类型没有处理,如Sql Server中的Float型等;主、外键的声明;对大对象类型没有处理等等等等,只是原理上大抵是如此。

对于MySql导出Insert语句,如果不是要移到其它类型的数据库上,用MySqlDump命令是OK,不用上面这么麻烦。

顺便抱怨一下各数据库的图型界面,Sql Server是OK的,企业管理器和查询分析器用起来很好。


对于MySql的图形界面,用的多的好像是“mysql front”,一个字,烂!只要数据库中表较多(几十张)或者表中数据较多(几千行)时,就死机(机器配置很好的前提下)。后来上mysql官网上下了 “mysql query browser”,一样有问题,首先,每次只能执行一条语句,其次,只要语句中有汉字,马上死,两个字,烂啊!


对于Oracle,不知道大家都在用什么界面,这里用的是“oraedit”,倒是不死,只是在编写存储过程或执行存储过程时,不时有莫名其妙的问题。

http://www.cnblogs.com/KissKnife/archive/2006/12/10/587997.html

上一页  [1] [2] 

我来说两句:

1分 2分 3分 4分 5分
姓名: *


* 请各位网友遵纪守法并注意语言文明。
网站简介 | 联系方式 | 意见建议 | 版权说明
Copyright © 2007 All rights reserved
滇ICP备06006992号