国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

openfiledialog讀取txt寫入數據庫示例

2020-01-24 02:45:58
字體:
來源:轉載
供稿:網友

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>
        /// 應用程序的主入口點。
        /// </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中的數據寫入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 參數
                            strIns = "insert into PhoneNo(TelNO,Name) values(@telNO,@name) ";
                            SqlParameter[] sqlPara = new SqlParameter[] {
                                    new SqlParameter("telNO",TelNo),
                                    new SqlParameter("name",Name)
                                };
                            //把讀取出來的數據寫入.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(); //參數清除
                                }
                                MessageBox.Show("導入成功", "Read TXT");
                            }
                        }
                    }
                }
            }
            else
            {
                return;
            }

        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上栗县| 游戏| 连州市| 温泉县| 阳高县| 南雄市| 星子县| 五指山市| 舞阳县| 秭归县| 磐安县| 吉木乃县| 扶余县| 辽中县| 嘉定区| 德清县| 莎车县| 双桥区| 剑阁县| 浙江省| 拉萨市| 玉溪市| 阜宁县| 滨海县| 防城港市| 昌邑市| 昭通市| 金沙县| 怀仁县| 敦化市| 云阳县| 潼关县| 葵青区| 马公市| 治县。| 固安县| 阜平县| 安义县| 神池县| 邓州市| 常宁市|