当前位置:  编程技术>.net/c#/asp.net

openfiledialog读取txt写入数据库示例

    来源: 互联网  发布时间:2014-10-29

    本文导语:  WinForm 中添加 openFileDialog Button, WinForm .cs 中添加本地.mdf,如下: 代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms; namespace txt记事本文件的读写{    static class Program    {        ///         /...

WinForm 中添加 openFileDialog Button, WinForm .cs 中添加本地.mdf,如下:

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace txt记事本文件的读写
{
    static class Program
    {
        ///
        /// 应用程序的主入口点。
        ///
        [STAThread]
        static void Main()
        {
            //SQLServer 附加mdf文件
            string dataDir = AppDomain.CurrentDomain.BaseDirectory;
            if (dataDir.EndsWith(@"binDebug") || dataDir.EndsWith(@"binRelease"))
            {
                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;
            }

        }
    }
}


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 如果我要用openFileDialog我应该import哪些?


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3