⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 default.aspx.cs

📁 将sql数据库中所有的表读出
💻 CS
字号:
//using System;
//using System.Data;
//using System.Configuration;
using System.Collections;
//using System.Web;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
//using System.Web.UI.HtmlControls;
using System.IO;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Xml;

public class zxml : Page
{

    private string connecString;
    private SqlConnection connection;


    public zxml()
    {
        //connecString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        connecString = "server=localhost;database=master;uid=sa1;pwd=223512";
        connection = new SqlConnection(connecString);

    }

    public ArrayList ShowFields(string tableName)
    {
        ArrayList list = new ArrayList();

        string columns = "select * from syscolumns where id=object_id('" + tableName + "')";


        SqlCommand command = new SqlCommand(columns, connection);
        connection.Open();

        SqlDataReader read = command.ExecuteReader();
        int count = 0;
        while (read.Read())
        {

            //tables += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;" + read[0].ToString();
            count++;
            list.Add(read[0].ToString());

        }

        read.Close();
        connection.Close();
        return list;



    }
    /// <summary>
    /// From Database To XML file
    /// </summary>
    /// <param name="tableName"></param>
    /// <param name="fields"></param>
    public void DatabaseToXml(string tableName, ArrayList fields)
    {
        
        connecString = "server=localhost;database=master;uid=sa;pwd= ";
        connection = new SqlConnection(connecString);
        FileStream fs = new FileStream(Server.MapPath("xml/") + tableName + ".xml", System.IO.FileMode.Create);
        XmlTextWriter textw = new XmlTextWriter(fs, System.Text.Encoding.UTF8);
        textw.Formatting = Formatting.Indented;
        textw.WriteStartDocument(true);


        int fieldCount = fields.Count;

        string sql = "select ";
        foreach (object sr in fields)
        {
            sql += (sr.ToString() + " ,");

        }
        sql = sql.Substring(0, sql.Length - 1);
        sql += (" from " + tableName);
        SqlCommand command = new SqlCommand(sql, connection);
        connection.Open();
        SqlDataReader re = command.ExecuteReader();
        textw.WriteStartElement(tableName);// write table name
        while (re.Read())
        {

            for (int i = 0; i < fieldCount; i++)
            {
                string element = re[fields[i].ToString()].ToString();
                textw.WriteElementString(fields[i].ToString(), element);

            }


        }
        re.Close();
        connection.Close();
        textw.WriteEndElement();
        textw.Close();
        //System.Diagnostics.Process.Start("iexplorer.exe", @"C:\Inetpub\wwwroot\webdata\xml\" + tableName + ".XML");
    }
    




}

public class Class1
{
    private string connecString;
    private SqlConnection connection;

    public Class1()
    {

        connecString = "server=localhost;database=master;uid=sa1;pwd=223512";
        connection = new SqlConnection(connecString);

    }


    public void WebMessageBox(string values)
    {
 
        HttpContext.Current.Response.Write("<script>alert('" + values + "');window.location.href='Default.aspx'</script>");

        HttpContext.Current.Response.End();
    }



}

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        if (DropDownList1.Items.Count == 0)
            new Class1().WebMessageBox("您先选择字段");



        string sValue = DropDownList1.SelectedItem.Value;

        ArrayList ds = new ArrayList();
        foreach (ListItem im in CheckBoxList1.Items)
        {
            if (im.Selected)
            {
                ds.Add(im.Value);

            }

        }
       //System.Diagnostics.Process.Start("iexplore.exe", @"Server.MapPath("xml/") + sValue + ".xml"");
       // System.Diagnostics.Process.Start("explorer.exe", @"");
        new zxml().DatabaseToXml(sValue, ds);
                //string path = @"Server.MapPath("xml/") + sValue + ".xml"";
        System.Diagnostics.Process.Start("explorer.exe", @"C:\Inetpub\wwwroot\webdata\xml");
        //string sr=
        // System.Diagnostics.Process.Start("iexplorer.exe", @"C:\Inetpub\wwwroot\webdata\xml\"+sValue+".xml");
             new Class1().WebMessageBox("好了 。。。。保存成功");
             
           

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

      

        Database datas = new Database();
        object[] table = datas.ShowTables();
        Label1.Text = table[0].ToString();
        ArrayList list = (ArrayList)table[1];
        DropDownList1.DataSource = list;
        DropDownList1.DataBind();

        GridView1.DataSource = list;
        GridView1.DataBind();
    }
    public void messagebox()
{

}
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectValue = DropDownList1.SelectedItem.Value;
               string tables = "";
        string columns = "select * from syscolumns where id=object_id('" + selectValue + "')";
        string connectString = "server=localhost;database=master;uid=sa1;pwd=223512";
        SqlConnection connection = new SqlConnection(connectString);
        ArrayList list1 = new ArrayList();
        SqlCommand command = new SqlCommand(columns, connection);
        connection.Open();

        SqlDataReader read = command.ExecuteReader();
        int count = 0;
        while (read.Read())
        {

            tables += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;" + read[0].ToString();
            list1.Add(read[0].ToString());
            count++;

        }
        tables = "您选择的表共有" + count + "个字段,具体表名如下:";
        read.Close();
        connection.Close();
        CheckBoxList1.DataSource = list1;
        CheckBoxList1.DataBind();
        Label2.Text = tables;
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
    {

    }
    protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        ArrayList list = new ArrayList();
        for (int t = 0; t < CheckBoxList1.Items.Count; t++)
            if (CheckBoxList1.Items[t].Selected)
            {
                list.Add(CheckBoxList1.Items[t].Text.ToString());
                GridView2.DataSource = list;
                GridView2.DataBind();


            }

        if (list.Count == 0)
            new Class1().WebMessageBox("请选择字段!");

    }
}
public class Database
{



    private string connectString = "server=localhost;database=master;uid=sa1;pwd=223512";
    private SqlConnection connection;


    public Database()
    {

        connection = new SqlConnection(connectString);

    }


    /// 查询出数据库里面的所有表

    public object[] ShowTables()
    {
        object[] tabletext = new object[2];
        string tables = "";
        string sql = "select name from sysobjects where xtype='U'";

        SqlCommand command = new SqlCommand(sql, connection);
        connection.Open();

        SqlDataReader read = command.ExecuteReader();
        System.Collections.ArrayList list = new System.Collections.ArrayList();
        int count = 0;
        while (read.Read())
        {

            //tables += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;" + read[0].ToString();
            list.Add(read[0].ToString());
            count++;

        }
        tables = "共有 " + count + "个表" + ",如下所示:";
        read.Close();
        connection.Close();
        tabletext[0] = tables;
        tabletext[1] = list;
        return tabletext;

    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -