黑马程序员_SQL Server数据的导入小结(1)
2012-03-19 13:54
316 查看
----------------------Windows Phone 7手机开发、.Net培训、期待与您交流!----------------------这些知识点小结是根据杨老师的讲课做了整理,如有不清楚的请看杨中科老师的视频教程:【传智播客.Net培训—ADO.Net】10案例数据导入导出SQL
Server中的数据导入导出
现在想把下列文本文件中的数据添加到数据库中:
Tom|23
Jack|28
Bob|36
创建好窗体项目后—>右击窗体项目—>添加—>新建项—>选择左边的“数据”—>再选择“基于服务的数据库”—>创建数据库—>创建表T_Persons用SQL语句创建表如:
create table T_Persons(Id int not null,Name nvarchar(50) null,Age int null) --把Id定义为主键接下来就是代码:
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 button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofdImport = new OpenFileDialog(); if (ofdImport.ShowDialog() == DialogResult.OK)
{
using (FileStream fileStream = File.OpenRead(ofdImport.FileName))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
string line = null;
while ((line = streamReader.ReadLine()) != null)
{
string[] str = line.Split('|');
string name = str[0];
int age = Convert.ToInt32(str[1]);
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\WinForm窗体练习\数据导入导出\DBImport_Out.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("成功导入!~");
}
}
}
}
}
}
----------------------Windows Phone 7手机开发、.Net培训、期待与您交流!----------------------
Server中的数据导入导出
现在想把下列文本文件中的数据添加到数据库中:
Tom|23
Jack|28
Bob|36
创建好窗体项目后—>右击窗体项目—>添加—>新建项—>选择左边的“数据”—>再选择“基于服务的数据库”—>创建数据库—>创建表T_Persons用SQL语句创建表如:
create table T_Persons(Id int not null,Name nvarchar(50) null,Age int null) --把Id定义为主键接下来就是代码:
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 button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofdImport = new OpenFileDialog(); if (ofdImport.ShowDialog() == DialogResult.OK)
{
using (FileStream fileStream = File.OpenRead(ofdImport.FileName))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
string line = null;
while ((line = streamReader.ReadLine()) != null)
{
string[] str = line.Split('|');
string name = str[0];
int age = Convert.ToInt32(str[1]);
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\WinForm窗体练习\数据导入导出\DBImport_Out.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("成功导入!~");
}
}
}
}
}
}
----------------------Windows Phone 7手机开发、.Net培训、期待与您交流!----------------------
相关文章推荐
- 黑马程序员_SQL Server数据导入小结(2)
- 关于SQL SERVER数据大容量的导入导出小结
- SQL SERVER导入、导出数据到Excel
- SQL Server往Oracle导入数据
- 数据 excel 导入 sql server
- mysql导入导出数据中文乱码解决方法小结
- SQL Server 2005使用SCP文本导入导出数据
- SQL server 导入数据提示“未在本地计算机上注册Mircrosoft.ACE.OLEDB.12.0 提供程序”的解决办法
- SQL SERVER数据导入到EXCEL
- 用sql server的脚本导入Excel2010或Excel2013格式数据
- 导入导出数据语句小结
- SQL Server 2005和SQL Server 2000数据的相互导入
- Excel数据导入SQL Server
- SQL Server数据导入导出技术概述与比较
- SQL Server中数据导入导出三种方法比较
- 数据库迁移(SQL SERVER导入数据到MySql)
- excel数据导入SQL SERVER
- 将Access数据库中数据导入到SQL Server中的详细方法实例
- 使用命令行将Excel数据表导入Mysql中的方法小结
- 黑马程序员--ADO.NET数据导入导出