using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Data.OleDb; using System.Data.SqlClient;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
static Int32 IPv4StringToInt32(string strIP) { IPAddress ipAddress; if (IPAddress.TryParse(strIP, out ipAddress)) { if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { byte[] bytes = ipAddress.GetAddressBytes(); return (Int32)(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]); } } return 0; }
private void WriteToDB_Click(object sender, EventArgs e) { //read from the txt file
System.IO.StreamReader read = new System.IO.StreamReader("E:\\IP\\ipsect.txt");
OleDbConnection Mycon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\IP\\ipdb.mdb"); Mycon.Open(); string create = @"create table IPtable(IP_Mask varchar(255), IP int, Mask int, Discription varchar(255))"; OleDbCommand CreateCmd = new OleDbCommand(create, Mycon); CreateCmd.ExecuteNonQuery(); //Mycon.Close();
string line; Int32 ip=0; Int32 mask = 0;
while ((line = read.ReadLine()) != null) { //line : allofchina 58.28.0.0/16
if (line.IndexOf('\t') >= 0) { string[] temp = line.Split('\t'); //temp[0]: allofchina temp[1]: 58.28.0.0/16
if (temp[1].IndexOf('/') >= 0) { string[] tempp = temp[1].Split('/'); //tempp[0] : 58.28.0.0 tempp[1] : 16
ip = IPv4StringToInt32(tempp[0]); mask = byte.Parse(tempp[1]); }
//temp[1](IP/Mask) ip(int) mask(int) temp[0](discription)
//write to the DB
string IP_Mask = temp[1]; string Discrip = temp[0]; string sql = @"INSERT INTO [IPtable]([IP_Mask], [IP], [Mask], [Discription]) VALUES ('"+IP_Mask+"', "+ip+", "+mask+", '"+Discrip+"')"; OleDbCommand cmd = new OleDbCommand(sql, Mycon); cmd.ExecuteNonQuery();
}//if
}//while
//OleDbCommand drop = new OleDbCommand("drop table IPtable", Mycon);
//drop.ExecuteNonQuery();
read.Close(); Mycon.Close(); } } }
|
这里在 保存ip的时候,由于access数据好像不支持无符号类型,所以就直接转化为有符号型进行保存的。除了转化为字符串外,不知道有没有其他的方法,存储这种32位无符号整数的方法,望知道的指教。
阅读(1951) | 评论(0) | 转发(0) |