微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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=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 < count; z++)
Button1.Text = "正在导出..";
Button1.Enabled = false;
lblInfo.Text = "正在导出第"+(z+1)+"组数据,共"+count+"组数据";
System.IO.StreamWriter file = new System.IO.StreamWriter(filePath+"\\" + (z+1) +"_"+fileName,false,Encoding.UTF8);
bool isFirst = true;
if (outType)
file.Write(@"<html><head><Meta http-equiv=content-type content=’text/html; charset=UNICODE’>
<style>*{font-size:12px;}table{background:#DDD;border:solid 2px #CCC;}td{background:#FFF;}
.th td{background:#EEE;font-weight:bold;height:28px;color:#008;}
div{border:solid 1px #DDD;background:#FFF;padding:3px;color:#00B;}</style>
<title>Export Table</title></head><body>");
file.Write("<table border=’0′ cellspacing=’1′ cellpadding=’3′>");
for (int i = size*z; i < dataTable.Rows.Count; i++)
System.Data.DaTarow daTarow = dataTable.Rows[i];
if (isFirst)
if ( outType)
file.Write("<tr class=’th’>");
for (int j = 0; j < dataTable.Columns.Count; j++)
file.Write("<td>");
file.Write(dataTable.Columns[j].ColumnName + "     ");
file.Write("</td>");
file.Write("</tr>");
isFirst = false;
file.Write("<tr>");
else
file.WriteLine(" ");
for (int k = 0; k < dataTable.Columns.Count; k++)
file.Write(dataTable.Rows[i][k] + "     ");
if (tmpNum==size)
break;
tmpNum += 1;
file.Write("</table>");
file.Write("<br /><div>执行成功!返回" + tmpNum + "行</div>");
file.Write("</body></html>");
file.WriteLine("执行成功!返回" + tmpNum + "行!");
file.dispose();
file.Close();
tmpNum = 1;
lblInfo.Text = "导出成功!";
Button1.Enabled = true;
Button1.Text = "开始导出";
catch (Exception ex)
lblInfo.Text = "导出失败!" + ex.Message;
lblInfo.ForeColor = System.Drawing.Color.Red;
}finally
connection.Close();
lblInfo.Text = "请先填写相关的连接信息!";
</script>
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
width: 61%;
.style2
height: 23px;
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" align=center>
sql Server 数据导 出          
By:<a href="http://blog.sina.com.cn/xi4oyu">MXi4oyu</a></td>
</tr>
<td>
服务器IP:</td>
<asp:TextBox ID="txtServerIP" runat="server" Width="172px"></asp:TextBox>
*</td>
数据库:</td>
<asp:TextBox ID="txtDatabase" runat="server" Width="172px"></asp:TextBox>
用户名:</td>
<asp:TextBox ID="txtUser" runat="server" Width="172px"></asp:TextBox>
密码:</td>
<asp:TextBox ID="txtPass" runat="server" Width="172px"></asp:TextBox>
表名:</td>
<asp:TextBox ID="txtTableName" runat="server" Width="172px"></asp:TextBox>
列名:</td>
<asp:TextBox ID="txtColName" runat="server" Width="172px"></asp:TextBox>
  列名之间请用‘,’分开,不写代表全部</td>
分组行数:</td>
<asp:TextBox ID="txtNum" runat="server" Width="172px"></asp:TextBox>
  对于数据多的时候可以使用</td>
保存文件名:</td>
<asp:TextBox ID="txtFileName" runat="server" Width="172px"></asp:TextBox>
文件格式:</td>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="type" Checked="true" Text="html" />
       
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="type" Text="txt" />
</td>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="开始导出" onclick="Button1_Click" />
<td colspan="2">
<asp:Label ID="lblInfo" runat="server" Text=""></asp:Label>
</table>
</div>
</form>
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐