设为首页 收藏本站
查看: 1020|回复: 0

[经验分享] mssql脱库脚本aspx版本

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-11-10 08:46:55 | 显示全部楼层 |阅读模式
<%@ 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+”组数据”;
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(@”<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;
if (isFirst)
{
if ( outType)
{
file.Write(“<tr class=’th’>”);
}
  
for (int j = 0; j < dataTable.Columns.Count; j++)
{
  
if (outType)
{
file.Write(“<td>”);
}
  
file.Write(dataTable.Columns[j].ColumnName + “     “);
  
if (outType)
{
file.Write(“</td>”);
}
}
  
if (outType)
{
file.Write(“</tr>”);
}
  
isFirst = false;
}
  
if (outType)
{
file.Write(“<tr>”);
}
else
{
file.WriteLine(” “);
}
  
for (int k = 0; k < dataTable.Columns.Count; k++)
{
  
if (outType)
{
file.Write(“<td>”);
}
  
file.Write(dataTable.Rows[k] + “     “);
  
if (outType)
{
file.Write(“</td>”);
}
}
  
if (outType)
{
file.Write(“<tr>”);
}
else
{
file.WriteLine(” “);
}
  
if (tmpNum==size)
break;
  
tmpNum += 1;
  
}
  
if (outType)
{
file.Write(“</table>”);
file.Write(“<br /><div>执行成功!返回” + tmpNum + “行</div>”);
file.Write(“</body></html>”);
}
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;
}
}
</script>
  
<html xmlns=”http://www.w3.org/1999/xhtml”>
<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 数据导 出&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
By:<a href=”http://hi.baidu.com/闪电小子_tysan”>闪电小子</a></td>
  
</tr>
<tr>
<td>
服务器IP:</td>
<td>
<asp:TextBox ID=”txtServerIP” runat=”server” Width=”172px”></asp:TextBox>
*</td>
</tr>
<tr>
<td>
数据库:</td>
<td>
<asp:TextBox ID=”txtDatabase” runat=”server” Width=”172px”></asp:TextBox>
*</td>
</tr>
<tr>
<td>
用户名:</td>
<td>
<asp:TextBox ID=”txtUser” runat=”server” Width=”172px”></asp:TextBox>
*</td>
</tr>
<tr>
<td>
密码:</td>
<td>
<asp:TextBox ID=”txtPass” runat=”server” Width=”172px”></asp:TextBox>
*</td>
</tr>
<tr>
<td>
表名:</td>
<td>
<asp:TextBox ID=”txtTableName” runat=”server” Width=”172px”></asp:TextBox>
*</td>
</tr>
<tr>
<td>
列名:</td>
<td>
<asp:TextBox ID=”txtColName” runat=”server” Width=”172px”></asp:TextBox>
&nbsp; 列名之间请用‘,’分开,不写代表全部</td>
</tr>
<tr>
<td>
分组行数:</td>
<td>
<asp:TextBox ID=”txtNum” runat=”server” Width=”172px”></asp:TextBox>
&nbsp; 对于数据多的时候可以使用</td>
</tr>
<tr>
<td>
保存文件名:</td>
<td>
<asp:TextBox ID=”txtFileName” runat=”server” Width=”172px”></asp:TextBox>
*</td>
</tr>
<tr>
<td>
文件格式:</td>
<td>
<asp:RadioButton ID=”RadioButton1″ runat=”server” GroupName=”type” Checked=”true” Text=”html” />
&nbsp; &nbsp; &nbsp; &nbsp;
<asp:RadioButton ID=”RadioButton2″ runat=”server” GroupName=”type” Text=”txt” />
</td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<asp:Button ID=”Button1″ runat=”server” Text=”开始导出” onclick=”Button1_Click” />
</td>
  
</tr>
<tr>
<td colspan=”2″>
<asp:Label ID=”lblInfo” runat=”server” Text=”"></asp:Label>
</td>
  
</tr>
</table>
  
</div>
</form>
</body>
</html>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-27400-1-1.html 上篇帖子: sql server 2012 merge的使用,同构表merger,异构表merge, 当merge 与trigger 同时使用时较容易出错 下篇帖子: SQL2000数据库同名覆盖数据恢复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表