openfiledialog讀取txt寫入數(shù)據(jù)庫示例
WinForm 中添加 openFileDialog Button, WinForm .cs 中添加本地.mdf,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace txt記事本文件的讀寫
{
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </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ù)寫入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記事本文件的讀寫
{
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)) //打開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)
};
//把讀取出來的數(shù)據(jù)寫入.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#使用DllImport調(diào)用非托管的代碼的方法
C#調(diào)用非托管代碼的方式主要有Com調(diào)用、DllImport方式調(diào)用、加載非托管動態(tài)鏈接庫、直接執(zhí)行機器碼等方式?,F(xiàn)在介紹一下我自己常用的DllImport方式調(diào)用MSDN中提到的GetShortPathName方法;2013-03-03
C#?System.Linq提供類似SQL語法的高效查詢操作
System.Linq是C#的一個命名空間,提供了LINQ(語言集成查詢)功能,允許開發(fā)者使用一致的查詢語法來處理不同類型的數(shù)據(jù)源,如數(shù)組、集合、數(shù)據(jù)庫和XML等,本文介紹C#?System.Linq提供類似SQL語法的高效查詢操作,感興趣的朋友一起看看吧2024-09-09
C#搜索TreeView子節(jié)點,保留父節(jié)點的方法
這篇文章主要介紹了C#搜索TreeView子節(jié)點,保留父節(jié)點的方法,實例分析了C#操作TreeView節(jié)點的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
C#使用yield關(guān)鍵字實現(xiàn)提升迭代性能與效率
yield關(guān)鍵字在C#中簡化了數(shù)據(jù)迭代的方式,實現(xiàn)了按需生成數(shù)據(jù),自動維護迭代狀態(tài),本文主要來聊聊如何使用yield關(guān)鍵字實現(xiàn)提升迭代性能與效率,感興趣的可以了解下2025-01-01
WPF利用TabControl控件實現(xiàn)拖拽排序功能
在UI交互中,拖拽操作是一種非常簡單友好的交互,這篇文章主要為大家介紹了WPF如何利用TabControl控件實現(xiàn)拖拽排序功能,需要的小伙伴可以參考一下2023-10-10
Winform動態(tài)加載TabControl用法實例
這篇文章主要介紹了Winform動態(tài)加載TabControl用法,以實例形式詳細講述了Winform動態(tài)加載TabControl的方法,在C#應(yīng)用程序開發(fā)中具有一定的實用價值,需要的朋友可以參考下2014-11-11

