• openFileDialog 打开TXT记事本文件写入数据库


    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;
                }
    
            }
        }
    }
  • 相关阅读:
    dp_Pku1887
    他们实际上控制的定义很easy5/12
    一:redis 的string类型
    我已经写了DAL层的代码生成器
    《Java并发编程实战》第十二章 测试并发程序 读书笔记
    [Pug] Template Engine -- Jade/ Pug
    [Angular] Alternative Themes
    [Angular] Separating Structural Styles From Theme Styles
    [React] Setup 'beforeunload' listener
    [Node] Convert CommonJS Requires to ES6 Imports
  • 原文地址:https://www.cnblogs.com/siri/p/2816912.html
Copyright © 2020-2023  润新知