• 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:UsersLGDesktop248zmhh1_zmhhSQLToExceladmInfo.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*8Desktop123.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>");
            }
        }
    }
  • 相关阅读:
    android 本地字符串存取
    2020-07-17:线上一个服务有4个实例突然变得访问很慢,你会从什么地方入手找原因?
    2020-07-16:如何获得一个链表的倒数第n个元素?
    2020-07-15:死锁与活锁的区别,死锁与饥饿的区别?
    2020-07-14:es用过冷热分离吗?假如现在有些数据热变冷,有些数据冷变热,怎么解决?
    2020-07-28:已知sqrt (2)约等于 1.414,要求不用数学库,求sqrt (2)精确到小数点后 10 位。
    2020-07-29:从 innodb 的索引结构分析,为什么索引的 key 长度不能太长?
    2020-07-27:如何设计一个分布式文件系统,如何设计动态扩容和数据定位?
    2020-07-26:如何用 socket 编程实现 ftp 协议?
    2020-07-25:如何实现一个高效的单向链表逆序输出?
  • 原文地址:https://www.cnblogs.com/wxylog/p/6144377.html
Copyright © 2020-2023  润新知