Dump SqlServer
%@ Page Language=C# % !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd script runat=server protected void Button1_Click(object sender, EventArgs e) { string serverIP=txtSer
protected void Button1_Click(object sender, EventArgs e)
{
string serverIP=txtServerIP.Text;
string database=txtDatabase.Text;
string user=txtUser.Text;
string pass=txtPass.Text;
string tableName=txtTableName.Text;
string colName=txtColName.Text;
string fileName=txtFileName.Text;
if (serverIP != null & database != null & user != null & pass != null & tableName != null & fileName != null)
{
string connectionString = "server="+serverIP+";database="+database+";uid="+user+";pwd="+pass;
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
try
{
connection.Open();
string sqlStr = "select * from "+tableName;
if (colName!="")
{
sqlStr = "select " + colName + " from " + tableName;
}
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlStr, connection);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
da.Fill(ds);
System.Data.DataTable dataTable = ds.Tables[0];
if (dataTable.Rows.Count==0)
{
lblInfo.Text = "没有需要导出的数据!";
lblInfo.ForeColor = System.Drawing.Color.Blue;
return;
}
string filePath = System.IO.Path.GetDirectoryName(Server.MapPath("DataOutExl.aspx"))+"\\DataOut";
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
bool outType = RadioButton1.Checked;
int sum = dataTable.Rows.Count;
int count = 1;
int size = 0;
int tmpNum = 1;
if (txtNum.Text!="")
{
size = int.Parse(txtNum.Text);
count = sum / size+1;
}
for (int z = 0; z
{
Button1.Text = "正在导出..";
Button1.Enabled = false;
lblInfo.Text = "正在导出第"+(z+1)+"组数据,共"+count+"组数据";
lblInfo.ForeColor = System.Drawing.Color.Blue;
System.IO.StreamWriter file = new System.IO.StreamWriter(filePath+"\\" + (z+1) +"_"+fileName, false, Encoding.UTF8);
bool isFirst = true;
if (outType)
{
file.Write(@"
.th td{background:#EEE;font-weight:bold;height:28px;color:#008;}
div{border:solid 1px #DDD;background:#FFF;padding:3px;color:#00B;}
file.Write("
}
for (int i = size*z; i
{
System.Data.DataRow dataRow = dataTable.Rows[i];
if (isFirst)
{
if ( outType)
{
file.Write("
}
for (int j = 0; j
{
if (outType)
{
file.Write("
}
file.Write(dataTable.Columns[j].ColumnName + " ");
if (outType)
{
file.Write("
}
}
if (outType)
{
file.Write("
}
isFirst = false;
}
if (outType)
{
file.Write("
}
else
{
file.WriteLine(" ");
}
for (int k = 0; k
{
if (outType)
{
file.Write("
}
file.Write(dataTable.Rows[i][k] + " ");
if (outType)
{
file.Write("
}
}
if (outType)
{
file.Write("
}
else
{
file.WriteLine(" ");
}
if (tmpNum==size)
break;
tmpNum += 1;
}
if (outType)
{
file.Write("");
");");
");");
");");
");
");");
file.Write("
file.Write("");
}
else
{
file.WriteLine("执行成功!返回" + tmpNum + "行!");
}
file.Dispose();
file.Close();
tmpNum = 1;
}
lblInfo.Text = "导出成功!";
lblInfo.ForeColor = System.Drawing.Color.Blue;
Button1.Enabled = true;
Button1.Text = "开始导出";
}
catch (Exception ex)
{
lblInfo.Text = "导出失败!" + ex.Message;
lblInfo.ForeColor = System.Drawing.Color.Red;
}finally
{
connection.Close();
}
}
else
{
lblInfo.Text = "请先填写相关的连接信息!";
lblInfo.ForeColor = System.Drawing.Color.Red;
}
}
.style1
{
width: 61%;
}
.style2
{
height: 23px;
}
SQL Server 数据导 出
By:MXi4oyu
服务器IP:
*
数据库:
*
用户名:
*
密码:
*
表名:
*
列名:
列名之间请用‘,’分开,不写代表全部
分组行数:
对于数据多的时候可以使用
保存文件名:
*
文件格式:
上一篇: php页面传递参数值几种方法总结
下一篇: php如何与mssql数据库连接与配置
推荐阅读
-
sqlserver交换数据行中的指定列
-
SCCM OSD安装前sqlserver&DC准备工作
-
Linux中生成Core Dump系统异常信息记录文件的教程
-
SqlServer2005中使用row_number()在一个查询中删除重复记录的方法
-
使用SQLSERVER 2005/2008 递归CTE查询树型结构的方法
-
SQLSERVER2005 中树形数据的递归查询
-
Win2008中SqlServer2008 无法打开错误日志文件导致无法启动的解决方法
-
sqlserver中获取当前日期的午夜的时间值的实现方法
-
关于sqlserver 2005 使用临时表的问题( Invalid object name #temptb)
-
shp2sqlserver 用法简析