黑马程序员之ADO.NET学习笔记:ADO.NET数据导入导出
来源:互联网 发布:买车的知乎 编辑:程序博客网 时间:2024/05/22 11:58
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
program.cs文件:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace 数据导入导出
{
static class Program
{
///<sumary>
///应用程序的主入口点
///</summary>
[STAThread]
static void Main()
{
string dateDir = 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());
}
}
}
数据导入导出.Form1文件:
using System;
using System.Collection.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 数据导入导出
{
public Form1()
{
InitializaComponent();
}
private void button1_Click(object sender,EventArgs e)
{
if(odfImport.ShowDialog() != DialogResult.OK)
{
return;
}
using(FileStream fileStream = File.OpenRead(odfImport.FileName))
{
using(StreamReaser streamReader = new StreamReader(fileStream))
{
using(SqlConnection conn = new SqlConnection(@"Data Source = .\SQLEXPRESS;AttachDBFilename=|DataDirectory|\MyDB.mdf;Integrated Security = True;User Instance = True"))
//创建连接是非常耗时的,因为不要每次操作都创建连接
{
conn.Open();
using(SqlCommand cmd = conn.CreateCommand())
{
string lines = null;
while(line = streamReader.ReadLine())! = null)
{
string[] strs = line.Split("|");
string name = strs[0];
int age = Convert.ToInt32(strs[1]);
cmd.Parameters.Clear();//!!!!参数不能重复添加,在while中一直用的就是一个SqlCommand对象
cmd.Parameters.Add(new SqlParameter("Name",name));
cmd.Parameters.Add(new SqlParameter("Age",name));
cmd.ExecuteNonQuery();
}
}
}
}
MessageBox.Show("导入成功!");
}
/*
if( odfImport.ShowDialog() == DialogResult.OK)
{
using(FileStream fileStream = File.OpenRead(odfImport.FileName))
{
using(StreamReaser 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|\MyDB.mdf;Integrated Security = True;User Instance = True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert into T_Person(Name,Age) values(@Name,@Age)";
cmd.Parameters.Add(new SqlParameter("Name",name));
cmd.Parameters.Add(new SqlParameter("Age",name));
cmd.ExecuteNonQuery();
}
}
}
}
}
MessageBox.Show("导入成功!");
}*/
}
}
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
- 黑马程序员之ADO.NET学习笔记:ADO.NET数据导入导出
- 黑马程序员--ADO.NET数据导入导出
- 黑马程序员之ADO.NET学习笔记:ADO.NET介绍
- 黑马程序员之ADO.NET学习笔记: SqlDataAdapter(数据适配器)
- 黑马程序员之ADO.NET学习笔记:ADO.NET学习总结
- 黑马程序员-- ADO.NET基础学习笔记
- 黑马程序员—学习笔记之ADO.Net
- 黑马程序员--学习笔记之ADO.Net基础知识
- 黑马程序员之ADO.NET学习笔记:登陆练习。
- 黑马程序员之ADO.NET学习笔记:SqlConnection(连接对象)
- 黑马程序员之ADO.NET学习笔记:SqlCommand(命令对象)
- 黑马程序员之ADO.NET学习笔记:SqlParameter(Sql参数)
- 黑马程序员之ADO.NET学习笔记:SqlDataReader(数据流读取器)
- 黑马程序员之ADO.NET学习笔记: SqlTransaction(事务)
- 黑马程序员之ADO.NET学习笔记:执行Insert操作
- 黑马程序员之ADO.NET学习笔记:执行select语句
- 黑马程序员之ADO.NET学习笔记:using和close
- 黑马程序员之ADO.NET学习笔记:ADO.NET如何读取Excel
- c编程规范和常范错误
- flow digram
- 大牛很通俗地介绍《信号与系统》
- 黑马程序员之ADO.NET学习笔记:登陆练习。
- 开源图形/游戏引擎的点评
- 黑马程序员之ADO.NET学习笔记:ADO.NET数据导入导出
- 接口与抽象类
- Windows Phone 8 SDK Preview将在9月12号开放
- Python字典与集合操作总结
- APK无法显示桌面快捷方式的原因
- 黑马程序员之ADO.NET学习笔记:IP地址归属地查询。查询结果:北京移动[海淀]。
- iOS App Launch Option
- poj2226 - Muddy Fields
- 黑马程序员之ADO.NET学习笔记:省市选择程序,数据全部来自于数据库。