019. Asp.net将SqlServer中的数据保存到xls/txt中

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string cmdtxt1 = "server=.;database=TYW;uid=sa;pwd=123456;";
            //ConfigurationSettings.AppSettings["strCon"];
            //定义查询所有的数据库的SQL语句
            string cmdtxt2 = "Exec sp_helpdb"; //Exec sp_helpdb 获取该服务器上所有数据库的库信息(包含库名/大小/所有者)
            //创建数据库连接对象
            SqlConnection Con = new SqlConnection(cmdtxt1);
            //打开数据库连接
            Con.Open();
            //创建命令对象
            SqlCommand mycommand = new SqlCommand(cmdtxt2, Con);
            //创建一个数据阅读器
            SqlDataReader dr = mycommand.ExecuteReader();
            ArrayList a = new ArrayList();
            while (dr.Read())
            {
                a.Add((String)dr["name"]);
                this.dropDatabase.DataSource = dr;
                this.dropDatabase.DataTextField = "name";
                this.dropDatabase.DataBind();
            }
            dr.Close();
           string cmdtxt3 = "use " + this.dropDatabase.SelectedValue + " SELECT * FROM " + this.dropDatabase.SelectedValue + ".dbo.sysobjects"; //切换到用户所选择的库, 然后查出所有该库中所有的表名
            cmdtxt3 += " WHERE xtype='U' AND STATUS>=0";    //并且类型为U(用户类型), 且状态大于等于0
            mycommand.CommandText = cmdtxt3;
            foreach (String Database in a)
            {
                Con.ChangeDatabase(Database);
                dr = mycommand.ExecuteReader();
                while (dr.Read())
                {
                    this.dropTable.DataSource = dr;
                    this.dropTable.DataTextField = "name";
                    this.dropTable.DataBind();
                }
                dr.Close();
            }
            //关闭数据库连接
            Con.Close();
        }
    }

    //更新对应的表名
    protected void dropDatabase_SelectedIndexChanged(object sender, EventArgs e)
    {
        string cmdtxt1 = "server=.;database=TYW;uid=sa;pwd=123456;";
        SqlConnection Con = new SqlConnection(cmdtxt1);
        Con.Open();
        string cmdtxt3 = "use " + this.dropDatabase.SelectedValue + " SELECT * FROM " + this.dropDatabase.SelectedValue + ".dbo.sysobjects";
        cmdtxt3 += " WHERE xtype='U' AND STATUS>=0";
        SqlCommand mycommand1 = new SqlCommand(cmdtxt3, Con);
        SqlDataReader dr1 = mycommand1.ExecuteReader();
        this.dropTable.DataSource = dr1;
        this.dropTable.DataTextField = "name";
        this.dropTable.DataBind();
        dr1.Close();
        Con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strFileData = Server.MapPath("123.txt");//C:\Users\LG\Desktop\24\08\zmhh\01_zmhh\SQLToExcel\admInfo.xls
       string cmdtxt1 = "server=.;database=TYW;uid=sa;pwd=123456;";
        //利用BCP实用工具将SQL Server数据库中数据导入到Excel文件中
        string cmdtxt2 = "USE master EXEC xp_cmdshell 'bcp " + this.dropDatabase.SelectedValue + ".dbo." + this.dropTable.SelectedValue + "";
        cmdtxt2 += " out " + strFileData + " -c -q -S. -Usa -P" + this.txtPwd.Text.Trim() + "'";
        //USE master EXEC xp_cmdshell 'bcp TYW.dbo.card out C:\Users\*8\Desktop\123.xls -c -q -S. -Usa -P123456'  利用这条语句将数据写入到文件中
        try
        {
            SqlConnection myconn = new SqlConnection(cmdtxt1);
            myconn.Open();
            SqlCommand Comd = new SqlCommand(cmdtxt2, myconn);
            Comd.ExecuteNonQuery();
            myconn.Close();
            Response.Write("<script language=javascript>alert('数据导入成功!');location='SQLToExcel.aspx'</script>");
        }
        catch (Exception ms)
        {
            Response.Write(ms.Message);
            Response.Write("<script language=javascript>alert('数据导入失败!');location='SQLToExcel.aspx'</script>");
        }
    }
}

 

posted on 2016-12-08 11:55  印子  阅读(216)  评论(0编辑  收藏  举报

导航