国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
            }

        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临海市| 依安县| 云南省| 准格尔旗| 定结县| 拜城县| 靖安县| 吴川市| 鄂托克旗| 仙居县| 湘潭县| 巫溪县| 车险| 浮山县| 伊金霍洛旗| 东明县| 安远县| 普陀区| 灵台县| 澄城县| 会同县| 南木林县| 阜城县| 化隆| 随州市| 秀山| 噶尔县| 沾益县| 庆城县| 平利县| 青铜峡市| 佳木斯市| 象州县| 芜湖市| 商城县| 建湖县| 灌南县| 罗源县| 铜鼓县| 崇阳县| 东莞市|