openfiledialog读取txt写入数据库示例

时间:2024-01-16 02:03:35 

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;
            }

        }
    }
}

标签:openfiledialog,数据库
0
投稿

猜你喜欢

  • 一文教你彻底解决Python包下载慢问题

    2023-04-02 20:29:32
  • 使用PHP获取网络文件的实现代码

    2023-09-09 08:41:53
  • Python关于__name__属性的含义和作用详解

    2021-10-28 09:29:51
  • 解决vue3打包过后空白页面的情况

    2024-05-09 10:43:00
  • 解决seaborn在pycharm中绘图不出图的问题

    2023-11-29 02:22:52
  • mysql分页的limit参数简单示例

    2024-01-19 05:58:31
  • 几种修复ACCESS数据库的实用方法

    2008-11-20 17:37:00
  • Python中的异常处理学习笔记

    2023-06-26 05:04:29
  • asp显示数据库中表名、字段名、字段内容

    2008-06-17 18:09:00
  • Django 限制访问频率的思路详解

    2021-08-17 16:52:57
  • PyTorch中反卷积的用法详解

    2022-09-21 18:12:34
  • ADO组件之插入数据记录

    2008-10-08 12:21:00
  • Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录

    2023-04-13 19:29:37
  • vue实现消息无缝滚动效果

    2024-05-29 22:42:33
  • mysql limit 分页的用法及注意要点

    2024-01-21 06:44:50
  • python中requests爬去网页内容出现乱码问题解决方法介绍

    2023-09-14 01:00:11
  • MySQL无法输入中文字符问题的解决办法

    2024-01-22 08:26:56
  • golang架构设计开闭原则手写实现

    2023-07-21 22:01:36
  • js实现正则匹配中文标点符号的方法

    2024-05-09 10:38:50
  • Python 程序员必须掌握的日志记录

    2021-11-05 06:17:20
  • asp之家 网络编程 m.aspxhome.com