ADO.NET导入文件到数据库4

来源:互联网 发布:php uploadify实例 编辑:程序博客网 时间:2024/04/29 21:15

首先新建一个文本 ,内容如下所示:

tom|30
jack|23
lucy|45
marry|21

在VS2010中新建一个Windows窗体应用程序,插入一个导入button和一个openFileDialog指针,如下图所示:


项目添加一个数据库文件 ,表文件设置如下:


Form1.cs代码如下:

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.IO;using System.Data.SqlClient;namespace 数据导入导出{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            /*            if (odfImport.ShowDialog() == DialogResult.OK)            {                using (FileStream fileStream = File.OpenRead(odfImport.FileName))                 {                    using (StreamReader streamReader = new StreamReader(fileStream))                    {                        string line = null;                        while ((line = streamReader.ReadLine())!= null)                        {                            string[] strs = line.Split('|');                            string name = strs[0];                            int age = Convert.ToInt32(strs[1]);                            using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;                    AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True")) //连接字符串                             {                                conn.Open();//打开数据库                                 using(SqlCommand cmd = conn.CreateCommand())                                {                                    cmd.CommandText = "Insert into T_Persons(Name,Age) values(@Name,@Age)";                                    cmd.Parameters.Add(new SqlParameter("Name", name));                                    cmd.Parameters.Add(new SqlParameter("Age", age));                                    cmd.ExecuteNonQuery();                                }                            }                         }                    }                }                MessageBox.Show("导入成功!");            }             */            //对以上导入文件程序进行代码重构优化:            if (odfImport.ShowDialog() == DialogResult.OK)            {                return;            }            using (FileStream fileStream = File.OpenRead(odfImport.FileName))            {                using (StreamReader streamReader = new StreamReader(fileStream))//StreamReader导入  StreamWrite导出                {                    using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;                    AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True")) //连接字符串                     {                        //创建连接是非常耗时的,因此不要每次操作都创建连接                        conn.Open();//打开数据库                         using (SqlCommand cmd = conn.CreateCommand())                        {                            cmd.CommandText = "Insert into T_Persons(Name,Age) values(@Name,@Age)";                            string line = null;                            while ((line = streamReader.ReadLine()) != null)                            {                                string[] strs = line.Split('|');                                string name = strs[0];                                int age = Convert.ToInt32(strs[1]);                                cmd.Parameters.Clear();//Parameters不能重复添加参数,在while中一直用的就是一个sqlcommand对象,要清除上一次调用cmd命令的步骤,不加这一条cmd.ExecuteNonQuery()会报错,                                cmd.Parameters.Add(new SqlParameter("Name", name));                                cmd.Parameters.Add(new SqlParameter("Age", age));                                cmd.ExecuteNonQuery();                            }                        }                    }                }            }            MessageBox.Show("导入成功");        }    }}



0 0
原创粉丝点击