基本信息
源码名称:C#读取数据库中表结构实例源码下载
源码大小:1.50M
文件格式:.zip
开发语言:C#
更新时间:2013-01-25
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

读取数据库中表结构实例


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SQLDMO.Application SQLServer = new SQLDMO.Application();
            SQLDMO.NameList strServerList = SQLServer.ListAvailableSQLServers();
            if (strServerList.Count > 0)
            {
                for (int i = 0; i < strServerList.Count; i  )
                {
                    toolStripTextBox1.Items.Add(strServerList.Item(i   1));
                }
            }
            toolStripTextBox1.Text = "(local)";
            comboBox2.SelectedIndex = 0;
            toolStripTextBox3.TextBox.PasswordChar = '*';
        }

        private void toolStripTextBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                toolStripTextBox3.Focus();
        }

        private void toolStripTextBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                toolStripButton1_Click(sender, e);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                string str = "Data Source="   toolStripTextBox1.Text   ";database=master;Uid="   toolStripTextBox2.Text   ";Pwd="   toolStripTextBox3.Text   ";";
                comboBox1.DataSource = getTable(str, "select name from sysdatabases", "sysdatabases");
                comboBox1.DisplayMember = "name";
                comboBox1.ValueMember = "name";
                button1.Enabled = button2.Enabled = button3.Enabled = true;
            }
            catch { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text != "")
            {
                listBox1.DataSource = null;
                listBox1.Items.Clear();
                string strCon = "Data Source="   toolStripTextBox1.Text   ";DataBase="   comboBox1.Text   ";uid="   toolStripTextBox2.Text   ";pwd="   toolStripTextBox3.Text;
                DataTable dt = null;
                if (comboBox2.Text == "数据表")
                {
                    dt = getTable(strCon, "SELECT name FROM sysobjects WHERE type = 'U' and name<>'dtproperties'", "sysobjects");
                }
                else if (comboBox2.Text == "视图")
                {
                    dt = getTable(strCon, "select name from sysobjects where xtype='v'", "sysobjects");
                }
                else if (comboBox2.Text == "存储过程")
                {
                    dt = getTable(strCon, "SELECT name FROM sysobjects WHERE xtype='p'", "sysobjects");
                }
                listBox1.DataSource = dt.DefaultView;
                listBox1.DisplayMember = "name";
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string strTableName = listBox1.SelectedValue.ToString();
                string strCon = "Data Source="   toolStripTextBox1.Text   ";DataBase="   comboBox1.Text   ";uid="   toolStripTextBox2.Text   ";pwd="   toolStripTextBox3.Text;
                using (SqlConnection con = new SqlConnection(strCon))
                {
                    string strSql = "select  name 字段名, xusertype 类型编号, length 长度 into hy_Linshibiao from  syscolumns  where id=object_id('"   listBox1.Text   "') ";
                    strSql  = "select name 类型,xusertype 类型编号 into angel_Linshibiao from systypes where xusertype in (select xusertype from syscolumns where id=object_id('"   listBox1.Text   "'))";
                    con.Open();
                    SqlCommand cmd = new SqlCommand(strSql, con);
                    cmd.ExecuteNonQuery();
                    SqlDataAdapter da = new SqlDataAdapter("select 字段名,类型,长度 from hy_Linshibiao t,angel_Linshibiao b where t.类型编号=b.类型编号", con);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt.DefaultView;
                    SqlCommand cmdnew = new SqlCommand("drop table hy_Linshibiao,angel_Linshibiao", con);
                    cmdnew.ExecuteNonQuery();
                    con.Close();
                }
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndices.Count > 0)
            {
                frmDataExport dataexport = new frmDataExport();
                dataexport.OutData = comboBox1.Text;
                dataexport.OutTable = listBox1.Text;
                dataexport.strserver = toolStripTextBox1.Text;
                dataexport.struser = toolStripTextBox2.Text;
                dataexport.strpwd = toolStripTextBox3.Text;
                dataexport.ShowDialog();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndices.Count > 0)
            {
                frmOutData outdata = new frmOutData();
                outdata.OutData = comboBox1.Text;
                outdata.OutTable = listBox1.Text;
                outdata.strserver = toolStripTextBox1.Text;
                outdata.struser = toolStripTextBox2.Text;
                outdata.strpwd = toolStripTextBox3.Text;
                outdata.ShowDialog();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private DataTable getTable(string strCon, string strSql, string strTable)
        {
            try
            {
                SqlConnection sqlcon = new SqlConnection(strCon);
                SqlDataAdapter da = new SqlDataAdapter(strSql, sqlcon);
                DataTable dt = new DataTable(strTable);
                da.Fill(dt);
                return dt;
            }
            catch
            {
                return null;
            }
        }
    }