asp.net the pure IP data into the code in the database

Source: Internet
Author: User
Tags add add time format eval net string table name tostring
Pure IP data contains 381,085, you can download the query software to extract the data into a text format, and its code to UTF8, otherwise in the program read Chinese will garbled!
The following screenshot of the program is performed to analyze IP data and insert into SQL Server:



The program uses AJAX to insert data into the client in real time updates:
The implementation code is as follows:
Front page and javascript:
Copy CodeThe code is as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Import IP Address database-power by blog.atnet.cc</title>
<style type= "Text/css" >
body{font-size:14px;}
#log {border:solid 1px gold;width:400px;height:100px;padding:10px;background:gold;margin-bottom:15px;color:black;}
#recordLog {font-size:12px;}
</style>
<script type= "Text/javascript" src= "/scripts/global.js" ></script>
<script type= "Text/javascript" >
var Log,relog; Log,recordlog
var RecordCount; Total IP Records
Window.onload=function () {
Log=document.getelementbyid ("Log");
}
function Startimport () {
if (!document.getelementbyid ("Submit_ifr")) {
var elem=document.createelement ("iframe");
Elem.setattribute ("id", "SUBMIT_IFR");
Elem.setattribute ("name", "IFR");
elem.style.csstext= "Display:none";
Document.body.appendChild (Elem);
Document.forms[0].target=elem.name;
}
Document.forms[0].submit ();
Log.innerhtml= "is uploading data!<br/>";
return false;
}

function Insertip () {
log.innerhtml+= "Start analyzing data ... <br/>";
J.ajax.post ("/do.ashx?args=importipdata&action=init", "",
function (x) {
var d=eval (x) [0];
Recordcount=d.count;
log.innerhtml+= "<font color=green> Analysis Data success: &LT;BR/> Server address:" +
D.server+ ", Record:" +recordcount+ "strip!<br/><div id=" Recordlog "></div>";
Start inserting
Insert ();
},
function (x) {log.innerhtml+= "<font color=red> occurred, terminated!</font>";}
);
}
function Insert () {
if (!relog) Relog=document.getelementbyid ("Recordlog");
var Num=math.floor (Math.random () *100);
J.ajax.post ("/do.ashx?args=importipdata&action=insert", "num=" +num,
function (x) {var d=eval (x) [0];relog.innerhtml=] has been written to the data: + (Recordcount-d.count) +
"Article, Queue:" +d.count+ "article, this time write:" +d.insertnum+ ";
if (d.count!=0) {insert ();}
Else{relog.innerhtml= "Congratulations, write complete!";}
},function (x) {alert (x);});
}
</script>
<body>
<div style= "margin:60px 100px" >
<div id= "Log" > Please fill in the relevant data, select the IP data file!</div>
<form action= "/do.ashx?args=importipdata" method= "post" enctype= "Multipart/form-data" target= "IFR" >
The database ip:<input type= "text" name= "DBServer value=". "/><br/>
Database name: <input type= "text" name= "dbname" value= "TP"/><br/>
Datasheet Name: <input type= "text" name= "tbname" value= "IP"/><br
User name: <input type= "text" name= "Dbuid" value= "sa"/><br/>
Password <input type= "password" name= "dbpwd" value= "123000″/><br/>"
IP files: <input type= "file" Name= "Ipfile" value= "C:\Users\cwliu\Desktop\1.txt"/><br/>
<button onclick= "return Startimport ();" > Import </button>
</form>
</div>
</body>

Note: J is a custom JavaScript class library with code in the middle that contains AJAX functionality
Background program we use to receive the POST request that Ajax sends:
The code is as follows:
Copy CodeThe code is as follows:
File:do.ashx?args=importipdata
public void ProcessRequest (HttpContext context)
{
if (context. Request.requesttype = "POST")
{
String action = context. Request["Action"];
Submitting IP Data
if (string. IsNullOrEmpty (action) action = "Submit")
{
String dbserver = context. request["DBServer"], Tbname = context. request["Tbname"];
StringBuilder sb = new StringBuilder (500);
Sb. Append ("server="). Append (DBServer). Append (";d atabase="). Append (context. request["dbname"])
. Append ("; uid="). Append (context. request["Dbuid"]). Append (";p wd="). Append (context. request["Dbpwd"]);
Save database connection string and data table name
httpcontext.current.session["ip_dbconnstring"] = sb. ToString ();
httpcontext.current.session["ip_tablename"] = Tbname;
Read IP data and cache
Ilist<string> iplist = new list<string> ();
Httppostedfile file = context. Request.files[0];
using (StreamReader sr = new StreamReader (file. InputStream, Encoding.UTF8))
{
while (Sr. Peek ()!=-1)
{
Iplist.add (Regex.Replace (Sr. ReadLine (), "\\s{2,}", ""));
}
}
HttpRuntime.Cache.Insert ("Ip_data", IPList);
Want to send data information to the client (JSON format)
Sb. Remove (0, sb.) Length);
Sb. Append ("[{server:"]). Append (DBServer)/server address
. Append ("", Count: ""). Append (iplist.count)//IP Bar number
. Append ("" ", insertnum:0")//This time inserting the number of bars
. Append (", tasknum:0")//Task queue number
. Append ("}]");
Context. session["Ip_info"] = sb. ToString ();
Triggers the parent page to start inserting data
Context. Response.Write ("<script>window.parent.insertip ();</script>");
}
Else
{
using (SqlConnection conn = new SqlConnection (context). session["Ip_dbconnstring"] as String)
{
String tbname = context. session["Ip_tablename"] as String;
Initializing, building a table and returning information
if (action = "Init")
{
SqlCommand cmd = new SqlCommand ("If Not EXISTS" (SELECT * from sysobjects where [name]= "" + Tbname +
"' and Xtype=" U ") BEGIN CREATE TABLE" + Tbname + "(ID BIGINT PRIMARY KEY IDENTITY (1,1), sip NVARCHAR, Eip NVARCHAR (a) Rea NVARCHAR (), [name] NVARCHAR) END ", conn);
Conn. Open ();
Cmd. ExecuteNonQuery ();
Context. Response.Write (context. session["Ip_info"]);
}
Inserting data
else if (action = "Insert")
{
Ilist<string> IPList = httpruntime.cache["Ip_data"] as ilist<string>;
StringBuilder sb = new StringBuilder (400);
Default to insert 300 bars each time
int insertnum;
Int. TryParse (context. request["Num"], out insertnum);
if (Insertnum < 1) insertnum = 300;
SqlCommand cmd = new SqlCommand ();
Cmd. Parameters.addrange (
New sqlparameter[]{
New SqlParameter ("@sip", null),
New SqlParameter ("@eip", null),
New SqlParameter ("@area", null),
New SqlParameter ("@name", NULL)
});
Cmd. Connection = conn;
Conn. Open ();
String[] arr;
for (var i = 0; I <= insertnum && i < iplist.count; i++)
{
arr = Iplist[i]. Split (")";
Cmd.commandtext = ' If not EXISTS ' (SELECT ID from + tbname +
"Where sip=" "+arr[0]+" "and eip=" "+arr[1]+" "INSERT into" + Tbname +
"Values (@sip, @eip, @area, @name)";
Cmd. parameters["@sip"]. Value = arr[0];
Cmd. parameters["@eip"]. Value = arr[1];
Cmd. parameters["@area"]. Value = arr[2];
Cmd. parameters["@name"]. Value =arr. LENGTH&GT;=4?ARR[3]: "";
Sb. Remove (0, sb.) Length);
Cmd. ExecuteNonQuery ();
Iplist.remove (Iplist[i]);
}
Sb. Remove (0, sb.) Length);
Sb. Append ("[{count:"). Append (Iplist.count)//not inserted IP number of bars
. Append (", Insertnum:"). Append (Insertnum)
. Append ("}]");
Context. Response.Write (sb.) ToString ());
}
}
}
}
}
}

When the above code is processed, the IP data is added to your database! Total is 380,000 add time in 1 hour or so!
The screenshot after writing to the database is as follows:


Related Article

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.