第一步:在“引用”項裡添加一個引用:System.configuration。
第二步:在項目裡右鍵->添加->建立項->添加一個“應用程式設定檔”即App.config檔案。
該檔案的代碼如下所示:
| 代碼如下 |
複製代碼 |
|
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ConnStr" connectionString="Data Source=.SQLEXPRESS;AttachDBFilename=|DataDirectory|Phone.mdf;
Integrated Security=True;User Instance=True"/>
</connectionStrings>
</configuration> |
第三步:在Program.cs頁面的Main()方法中添加如下串連ADO.Net中建立資料庫的代碼:
其作用:這段代碼時用於讀取AD.Net下建立的資料庫,不加則讀取根目錄Debug下的資料庫
| 代碼如下 |
複製代碼 |
|
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"binDebug")
|| dataDir.EndsWith(@"binRelease"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
} |
第四步:在項目裡右鍵->添加->建立項->添加一個“基於服務的資料庫”(資料庫名為:Phone.mdb);資料庫有七個欄位:Id(標識符),StartNo(開始號碼),EndNo(結束號碼),Sheng(號碼所在省、自治區或直轄市),Shi(號碼所在市),TMobile(電訊廠商),CardType(卡的類型)。
最後,在表單Form1.cs頁面中添加兩個using命令:using System.IO;和using System.Data.SqlClient;然後,在表單上拖出一個按鈕:用於單擊時匯入資料。
該頁面的完整代碼如下:
| 代碼如下 |
複製代碼 |
|
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.IO;
using System.Data.SqlClient;
namespace 手機號碼歸屬地
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnImport_Click(object sender, EventArgs e)
{
if (ofdImport.ShowDialog() != DialogResult.OK)
{
return;
}
//開啟現有檔案以進行讀取
//using (FileStream fileStream = new FileStream(ofdImport.FileName, FileMode.Open,
// FileAccess.Read,FileShare.None))
using (FileStream fileStream = File.OpenRead(ofdImport.FileName))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.SQLEXPRESS;AttachDBFilename=" +
AppDomain.CurrentDomain.BaseDirectory + "Phone.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"))
{
//建立串連非常耗時,因此不要每次操作都建立串連
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert into T_PhoneNum(StartNo,EndNo,Sheng,Shi,TMobile,CardType)values(@StartNo,@EndNo,@Sheng,@Shi,@TMobile,@CardType)";
string line = null;
while ((line = streamReader.ReadLine()) != null)//先在()中給line賦值,然後再進行比較
{
string[] strs = line.Split('-');
string startNo = strs[0];
string endNo = strs[1];
string sheng = strs[2];
string shi = strs[3];
string merchant = strs[4];
string type = strs[5];
//參數不能重複添加,在while中一直用的就是同一個SqlCommand對象
cmd.Parameters.Clear();//每次添加參數前先清除原先上次執行的參數
cmd.Parameters.Add(new SqlParameter("StartNo", startNo));
cmd.Parameters.Add(new SqlParameter("EndNo", endNo));
cmd.Parameters.Add(new SqlParameter("Sheng", sheng));
cmd.Parameters.Add(new SqlParameter("Shi", shi));
cmd.Parameters.Add(new SqlParameter("TMobile", merchant));
cmd.Parameters.Add(new SqlParameter("CardType", type));
cmd.ExecuteNonQuery();//假如不清空原先資料,在此會出現SqlException異常(變數名'@Name'已聲明)
}
}
}
}
}
MessageBox.Show("資料匯入成功!");
}
}
} |