openfiledialog讀取txt寫(xiě)入數(shù)據(jù)庫(kù)示例
WinForm 中添加 openFileDialog Button, WinForm .cs 中添加本地.mdf,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace txt記事本文件的讀寫(xiě)
{
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
//SQLServer 附加mdf文件
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
讀取txt中的數(shù)據(jù)寫(xiě)入DB:
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.Data.SqlClient;
using System.IO;
namespace txt記事本文件的讀寫(xiě)
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnReadTXT_Click(object sender, EventArgs e)
{
if (odfImport.ShowDialog() == DialogResult.OK)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TelphoneNo.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (FileStream fileStream = File.OpenRead(odfImport.FileName)) //打開(kāi)txt文件
{
using (StreamReader stmReader = new StreamReader(fileStream)) //讀取txt文件
{
string line = null;
string TelNo = "";
string Name = "";
string strIns = "";
//sql 參數(shù)
strIns = "insert into PhoneNo(TelNO,Name) values(@telNO,@name) ";
SqlParameter[] sqlPara = new SqlParameter[] {
new SqlParameter("telNO",TelNo),
new SqlParameter("name",Name)
};
//把讀取出來(lái)的數(shù)據(jù)寫(xiě)入.mdf
using (SqlCommand sqlCmd = new SqlCommand(strIns, conn))
{
//逐行讀取
while ((line = stmReader.ReadLine()) != null)
{
string[] strTel = line.Split('-');
TelNo = strTel[0].ToString();
Name = strTel[1].ToString();
sqlCmd.Parameters.AddRange(sqlPara);
sqlCmd.ExecuteNonQuery();
sqlCmd.Parameters.Clear(); //參數(shù)清除
}
MessageBox.Show("導(dǎo)入成功", "Read TXT");
}
}
}
}
}
else
{
return;
}
}
}
}
相關(guān)文章
C#連接SQL?Sever數(shù)據(jù)庫(kù)詳細(xì)圖文教程
C#是Microsoft公司為.NET Framework推出的重量級(jí)語(yǔ)言,和它搭配最完美的數(shù)據(jù)庫(kù)無(wú)疑就是Microsoft SQL Server了,下面這篇文章主要給大家介紹了關(guān)于C#連接SQL?Sever數(shù)據(jù)庫(kù)的詳細(xì)圖文教程,需要的朋友可以參考下2023-06-06c# 通過(guò)經(jīng)緯度查詢 具體的地址和區(qū)域名稱(chēng)
最近項(xiàng)目需要通過(guò)經(jīng)緯度查詢 具體的地址和區(qū)域名稱(chēng),通過(guò)查詢網(wǎng)絡(luò)資源,發(fā)現(xiàn)提供的大多是得到具體的地址而對(duì)區(qū)域或城市名稱(chēng)的獲取就不是很好把握;在這里自己搞了個(gè),需要的朋友可以參考下2012-11-11c#刪除數(shù)組中符合條件的元素(正確寫(xiě)法)
這篇文章主要介紹了c#刪除數(shù)組中符合條件的元素,分別給大家展示了錯(cuò)誤寫(xiě)法和正確寫(xiě)法,補(bǔ)充介紹了從C#的數(shù)組中刪除指定元素的幾種方法,需要的朋友可以參考下2023-10-10C#中如何自定義配置上周和本周起始日來(lái)查詢業(yè)務(wù)數(shù)據(jù)(思路詳解)
在C#中并沒(méi)有封裝的方法根據(jù)我們需要來(lái)直接獲取上一周某天到某天、本周某天到某天,所以需要我們自己封裝方法來(lái)實(shí)現(xiàn)(我們也可以按照這個(gè)思路使用其他語(yǔ)言來(lái)實(shí)現(xiàn)),感興趣的朋友跟隨小編一起看看吧2023-09-09利用TaskManager爬取2萬(wàn)條代理IP實(shí)現(xiàn)自動(dòng)投票功能
話說(shuō)某天心血來(lái)潮想到一個(gè)問(wèn)題,朋友圈里面經(jīng)常有人發(fā)投票鏈接,讓幫忙給XX投票,以前呢會(huì)很自覺(jué)打開(kāi)鏈接幫忙投一票。可是這種事做多了就會(huì)考慮能不能使用工具來(lái)進(jìn)行投票呢,身為一名程序猿決定研究解決這個(gè)問(wèn)題。感興趣的朋友一起學(xué)習(xí)吧2015-09-09C#讀寫(xiě)配置文件方式(config.ini)入門(mén)
這篇文章主要介紹了C#讀寫(xiě)配置文件方式(config.ini)入門(mén),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06