MSSQL database detaching script aspx version

Source: Internet
Author: User

<% @ 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 button#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 = Ds. Tables [0];

If (datatable. Rows. Count = 0)
{
Lblinfo. Text = "no data to be exported !";
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 = "exporting ..";
Button1.enabled = false;
Lblinfo. Text = "exporting" + (Z + 1) + "group data, total" + Count + "group data ";
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> <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>

File. Write ("<Table border = '0' cellspacing = '1' cellpadding = '3'> ");

}

For (INT I = size * z; I <datatable. Rows. Count; I ++)
{
System. Data. datarow = datatable. Rows [I];
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 [I] [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> execution successful! Return "+ tmpnum +" Row </div> ");
File. Write ("</body> }
Else
{
File. writeline ("execution successful! Return "+ tmpnum +" line !");
}

File. Dispose ();
File. Close ();
Tmpnum = 1;
}

Lblinfo. Text = "exported successfully !";
Lblinfo. forecolor = system. Drawing. color. blue;
Button1.enabled = true;
Button1.text = "start exporting ";

}
Catch (exception ex)
{
Lblinfo. Text = "Export failed !" + Ex. message;
Lblinfo. forecolor = system. Drawing. color. Red;

} Finally
{
Connection. Close ();
}

}
Else
{
Lblinfo. Text = "please fill in the relevant connection information first !";
Lblinfo. forecolor = system. Drawing. color. Red;
}
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/5o/xhtml">
<Head runat = "server">
<Title> No title page </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 data export & nbsp;
By: <a href = "http://hi.baidu.com/lightning kid_tysan”> lightning kiddies </a> </TD>

</Tr>
<Tr>
<TD>
Server IP: </TD>
<TD>
<Asp: textbox id = "txtserverip" runat = "server" width = "172px"> </ASP: textbox>
* </TD>
</Tr>
<Tr>
<TD>
Database: </TD>
<TD>
<Asp: textbox id = "txtdatabase" runat = "server" width = "172px"> </ASP: textbox>
* </TD>
</Tr>
<Tr>
<TD>
Username: </TD>
<TD>
<Asp: textbox id = "txtuser" runat = "server" width = "172px"> </ASP: textbox>
* </TD>
</Tr>
<Tr>
<TD>
Password: </TD>
<TD>
<Asp: textbox id = "txtpass" runat = "server" width = "172px"> </ASP: textbox>
* </TD>
</Tr>
<Tr>
<TD>
Table Name: </TD>
<TD>
<Asp: textbox id = "txttablename" runat = "server" width = "172px"> </ASP: textbox>
* </TD>
</Tr>
<Tr>
<TD>
Column name: </TD>
<TD>
<Asp: textbox id = "txtcolname" runat = "server" width = "172px"> </ASP: textbox>
& Nbsp; separate column names with commas (,). If this parameter is left blank, it indicates all columns. </TD>
</Tr>
<Tr>
<TD>
Number of groups: </TD>
<TD>
<Asp: textbox id = "txtnum" runat = "server" width = "172px"> </ASP: textbox>
& Nbsp; can be used when there are too many data </TD>
</Tr>
<Tr>
<TD>
Save file name: </TD>
<TD>
<Asp: textbox id = "txtfilename" runat = "server" width = "172px"> </ASP: textbox>
* </TD>
</Tr>
<Tr>
<TD>
File Format: </TD>
<TD>
<Asp: radiobutton id = "radiobutton1" runat = "server" groupname = "type" Checked = "true" text = "html"/>
& 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 = "start to export" onclick = "button#click"/>
</TD>

</Tr>
<Tr>
<TD colspan = "2">
<Asp: Label id = "lblinfo" runat = "server" text = ""> </ASP: Label>
</TD>

</Tr>
</Table>

</Div>
</Form>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.