欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

openfiledialog读取txt写入数据库示例

时间:2021-04-13 09:12:13|栏目:.NET代码|点击:

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

        }
    }
}

上一篇:ASP.NET 2.0服务器控件开发之复杂属性

栏    目:.NET代码

下一篇:ASP.NET中为DataGrid添加合计字段

本文标题:openfiledialog读取txt写入数据库示例

本文地址:http://www.codeinn.net/misctech/100106.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有