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
- ADO.NET导入文件到数据库4
- 通过ADO.NET把文件数据导入到数据库(数据的导入导出)stream文件的处理
- .Net中导入Excel文件到数据库
- 导入文件到数据库
- asp.net导入EXCEL内容文件到数据库
- ASP.NET 从Excel文件导入数据到数据库(笔记)
- c#.net excel文件导入到数据库 之一
- ASP.NET MVC导入excel到数据库,下载文件
- ADO 数据库的 导出和导入实例(从数据表导入到文件和把文本文件导入到数据库的数据表中)
- .net excel导入到数据库
- .net excel导入到数据库
- 导入文件到db2数据库
- 导入dmp文件到数据库
- 导入文件内容到数据库
- 导入dmp文件到数据库
- 导入数据(ADO.Net)
- ADO.NET控制图片数据,导入导出数据库
- asp.net EXCEL数据 导入到数据库
- 中国古代窗户 采光 不用纸 而是明瓦
- hdoj 1072 Nightmare
- 使用linux的alternatives管理多版本的软件
- .NET Interceptor with Dynamic Proxy
- Jstl简介
- ADO.NET导入文件到数据库4
- 3行程序搞定SVM分类-用JAVA程序调用LibSVM API 最简单的示例
- 对收录的文件进行MD5校验出错
- eclipse 设置maven来自动下载源码与doc
- Android基础
- AFNetworking发送成功,接收不到数据问题
- Spring整合Hessian
- STM32F429--LTDC寄存器详解
- 进程的查看、创建、会话、守护进程、结束