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>");
}
}
}
019. Asp.net将SqlServer中的数据保存到xls/txt中的更多相关文章
- c# 将datatable中的数据保存到excel文件中
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...
- 我用C#调用C编译的dll中有这样一个函数,函数大概的功能就是把数据保存到buf缓冲区中:
我用C#调用C编译的dll中有这样一个函数,函数大概的功能就是把数据保存到buf缓冲区中: C/C++ code ? 1 int retrieve(int scanno,void* buf); 在 ...
- mysql触发器的使用 想让b字段在更新的时候把旧数据保存到a字段中
使用mysql希望数据库自动触发一些规则,进行更新数据的时候,就需要用触发器了,比如 将旧数据保存到额外字段中,如何做呢? 在abc表中 name更新的时候 我希望把name的老数据保存到 old_n ...
- 使用scrapy爬取的数据保存到CSV文件中,不使用命令
pipelines.py文件中 import codecs import csv # 保存到CSV文件中 class CsvPipeline(object): def __init__(self): ...
- C#中 将图片保存到Sql server 中
private void Form1_Load(object sender, EventArgs e) { #region 保存数据库 string url = @"C:\Users\Adm ...
- 直接把数据库中的数据保存在CSV文件中
今天突然去聊就来写一个小小的demo喽,嘿嘿 public partial class Form1 : Form { public Form1() { InitializeComponent(); } ...
- PHP操作:将数据库中的数据保存到Word、Excel中。
1.首先要把word.excel表放到文件的根目录下 2.定义了一个word类 <?php class word { function start() { ob_start(); ob_star ...
- 快速将excel数据保存到Oracle数据库中【转】
我们在工作中,也许会碰到以下情况,客户或者同事发来需要调查的数据,并不是dmp文件,而是excel文件,此时通常是一张表,少量几条记录.最近我恰好碰到了这种情况,所以做了些调查,不敢藏私,拿出来跟大家 ...
- 将 node.js 的数据保存到 mongo 数据库中
Mongo 数据库 安装 首先到 Mongo 的官方网站下载安装程序:http://www.mongodb.org/,我下载的文件名为:mongodb-win32-x86_64-2008plus-2. ...
随机推荐
- HD OJ2023
#include "stdio.h"double stu[60],cla[10];int a[60][60];int main(){ int n,m,i,number,j; whi ...
- C++11 不抛异常的new operator
在google cpp style guide里面明确指出:we don't use exceptions C++11的noexcept关键字为这种选择提供了便利. C++11以前,提及malloc和 ...
- Oracle 获取用户表的字段定义
获取用户表列表: select * from user_tables; select * from all_tables; select * from dba_tables; 获取表的字段: sele ...
- Best Practice: Avoiding or minimizing synchronization in servlets
Introduction Minimize the use of synchronization in servlets. Because servlets are multi-threaded, s ...
- IOS源码封装成.bundle和.a文件时,使用单例作为出口的写法!任何封装都建议使用这种方法作为出口
头文件 以此作为模板,记录于此 #import <Foundation/Foundation.h>#import <UIKit/UIKit.h>//this can write ...
- Zipper_DP
Description Given three strings, you are to determine whether the third string can be formed by comb ...
- magento首页调用最新产品
这个需要我们自己添加一个block块供我们调用,可参考new products的block类,建立文件app/code/core/Mage/Catalog/Block/Product/Special. ...
- nginx下面部署fast-cgi和C++【原】
1.cgi文件的代码 #include "fcgi_stdio.h" #include <stdlib.h> #include <stdio.h> int ...
- I.MX6 console 跳过 login
/*************************************************************************** * I.MX6 console 跳过 logi ...
- vs2013 c++代码内出现中文导致编译错误
简单的做法就是,首先,菜单栏->文件->高级保存选项,选择utf-8 无签名, 然后,如果是发现注释语句里有中文,可以让注释语句与下行代码中间空一行, 如果是代码里有用到中文,那么就在中文 ...