IP地址TXT格式转换为MDB的小工具

该博客展示了一个IP地址“TXT->MDB”转换器的C#代码。程序通过Windows窗体设计,可将“QQ IP数据库纯真版”导出的文本数据转换为Access数据库。用户点击“浏览转换文件”按钮选择TXT文件,程序会将数据插入到MDB文件中,并显示转换的计数器、计时器和速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
namespace IPtxt_mdb
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button butIPfrom;
private System.Windows.Forms.TextBox txtIPfrom;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbCommand oleDbCommand1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.butIPfrom = new System.Windows.Forms.Button();
this.txtIPfrom = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// butIPfrom
//
this.butIPfrom.Location = new System.Drawing.Point(312, 8);
this.butIPfrom.Name = "butIPfrom";
this.butIPfrom.Size = new System.Drawing.Size(88, 23);
this.butIPfrom.TabIndex = 0;
this.butIPfrom.Text = "浏览转换文件";
this.butIPfrom.Click += new System.EventHandler(this.butIPfrom_Click);
//
// txtIPfrom
//
this.txtIPfrom.Location = new System.Drawing.Point(80, 8);
this.txtIPfrom.Name = "txtIPfrom";
this.txtIPfrom.Size = new System.Drawing.Size(224, 21);
this.txtIPfrom.TabIndex = 2;
this.txtIPfrom.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 3;
this.label1.Text = "转换文件:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = @"Jet OLEDB:Registry Path=;Data Source=""IPData.mdb"";Jet OLEDB:System database=;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:Database Locking Mode=1;Jet OLEDB:SFP=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Global Partial Bulk Ops=2;Mode=Share Deny None;Extended Properties=;Jet OLEDB:Engine Type=5";
//
// oleDbCommand1
//
this.oleDbCommand1.CommandText = "INSERT INTO ip (StartIP, EndIP, Address) VALUES (?, ?, ?)";
this.oleDbCommand1.Connection = this.oleDbConnection1;
this.oleDbCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("StartIP", System.Data.OleDb.OleDbType.Double, 0, "StartIP"));
this.oleDbCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("EndIP", System.Data.OleDb.OleDbType.Double, 0, "EndIP"));
this.oleDbCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Address", System.Data.OleDb.OleDbType.VarWChar, 250, "Address"));
//
// textBox1
//
this.textBox1.Enabled = false;
this.textBox1.Location = new System.Drawing.Point(56, 40);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(64, 21);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Enabled = false;
this.textBox2.Location = new System.Drawing.Point(184, 40);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(104, 21);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 8;
this.label2.Text = "计数器:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.Location = new System.Drawing.Point(136, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 23);
this.label3.TabIndex = 9;
this.label3.Text = "计时器:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// textBox3
//
this.textBox3.Enabled = false;
this.textBox3.Location = new System.Drawing.Point(344, 40);
this.textBox3.Name = "textBox3";
this.textBox3.ReadOnly = true;
this.textBox3.Size = new System.Drawing.Size(56, 21);
this.textBox3.TabIndex = 10;
this.textBox3.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(304, 40);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(48, 23);
this.label4.TabIndex = 9;
this.label4.Text = "速度:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 77);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtIPfrom);
this.Controls.Add(this.butIPfrom);
this.Controls.Add(this.label4);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.Text = "IP地址“TXT->MDB”转换器";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void butIPfrom_Click(object sender, System.EventArgs e)
{
this.Text="IP地址正在进行转换,请稍候......";
string filename="IPData.mdb";
//将模板文件复制到一个新文件中,
//实际使用时可以生成一个临时文件而无须用户选择。
//SaveFileDialog mySave=new SaveFileDialog();
//mySave.Filter="Excel文件(*.XLS)|*.xls|所有文件(*.*)|*.*";
//if(mySave.ShowDialog()!=DialogResult.OK)
//{
//return;
//}
//else
//{
//filename=mySave.FileName;
//复制刚才创建的模板文件为新文件,建议将模
//板文件放入数据库中,使用时将文件下载到本地
FileInfo mode=new FileInfo("ip模板.mdb");
try
{
mode.CopyTo(filename,true);
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
return;
}
//}
openFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
openFileDialog1.FilterIndex = 1 ;
openFileDialog1.RestoreDirectory = true ;

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string line;
using (StreamReader sr=new StreamReader(openFileDialog1.FileName,System.Text.Encoding.Default))
{
txtIPfrom.Text=openFileDialog1.FileName;
/*System.IO.TextReader txtreader;
txtreader=System.IO.File.OpenText(txtIPfrom.Text);
richTextBox1.Text=txtreader.ReadToEnd();
txtreader.Close();*/
// Insert code to read the stream here.
string sip,eip,addr;
uint SIP,EIP,i=0;
string[] szuip,ezuip;
DateTime nowfrom,nowto;
TimeSpan fromto;
this.oleDbCommand1.Connection.Open();
nowfrom=DateTime.Now;//.ToString("HH:mm:ss");

while((line = sr.ReadLine()) != null)
{
sip=line.Substring(0,16).Trim();
szuip=sip.Split('.');
SIP=(uint.Parse(szuip[0]))*256*256*256+(uint.Parse(szuip[1]))*256*256+(uint.Parse(szuip[2]))*256+(uint.Parse(szuip[3]));

eip=line.Substring(16,16).Trim();
ezuip=eip.Split('.');
EIP=(uint.Parse(ezuip[0]))*256*256*256+(uint.Parse(ezuip[1]))*256*256+(uint.Parse(ezuip[2]))*256+(uint.Parse(ezuip[3]));

addr=line.Substring(32,line.Length-32).Trim();

this.oleDbCommand1.Parameters["StartIP"].Value=SIP;
this.oleDbCommand1.Parameters["EndIP"].Value=EIP;
this.oleDbCommand1.Parameters["Address"].Value=addr;
this.oleDbCommand1.ExecuteNonQuery();
i=i+1;

}
this.oleDbCommand1.Connection.Close();
textBox1.Text=i.ToString();
nowto=DateTime.Now;//.ToString("HH:mm:ss");
fromto=nowto-nowfrom;
textBox2.Text=fromto.ToString();
textBox3.Text=Convert.ToString(i/(Single)(fromto.Ticks*0.0000001));
//textBox3.Text=Convert.ToString(i/(fromto.Hours*3600+fromto.Minutes*60+fromto.Seconds+fromto.Milliseconds*0.001));
//MessageBox.Show(Convert.ToString(i/Convert.ToSingle(fromto.Ticks*0.0000001))+"="+i/(fromto.Hours*3600+fromto.Minutes*60+fromto.Seconds+fromto.Milliseconds*0.001));
}
}
catch(Exception err)
{
MessageBox.Show("因“QQ IP数据库纯真版”导出的文本结尾有说明文字,所以"+err.Message+"/n数据已经成功转换完毕!");
}
this.Text="数据已经成功转换完毕!";
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
MessageBox.Show("  此程序为“QQ IP数据库纯真版”程序导出的文/n本数据转换为Access数据库的小工具。/n  主要用于网站留言版和论坛中的用户IP查地址/n功能。还可以用于测试计算机的速度哦 :)/n/n             豹子制作于2005-6-15 /n             http://www.wnest.com","说明",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值