C#批量导入text到数据库sqlserver
2013-09-24 16:54
190 查看
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;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.label1.Text = loadData().ToString();
}
public string randStr(int count)
{
string allchar = "1,2,3,4,5,6,7,8,9,0";
string[] allchararray = allchar.Split(',');
string randomcode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < count; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(10);
if (temp == t)
{
return randStr(count);
}
temp = t;
randomcode += allchararray[t];
}
return randomcode;
}
public int loadData()
{
StreamReader sr = new StreamReader(new FileStream("data.txt", FileMode.Open, FileAccess.Read), System.Text.Encoding.Default);
SqlConnection cn = new SqlConnection("Server=.;uid=sa;pwd=1122;database=vehicle"); ;
string insertCmd = "insert into infovehicle(Vehiclenum,vehicleid,sim) values (@Vehiclenum,@id, @sim)";
char delimChar = '\t';
string[] split = null;
int count = 0;
string strTemp = sr.ReadLine();
string strName=null;
string err="";
while (strTemp != null)
{
split = strTemp.Split(delimChar);
SqlCommand cmd = new SqlCommand(insertCmd, cn);
count++;
strName ="V"+ DateTime.Now.ToString("yyyyMMddhhmmss").ToString()+randStr(4);
string temp1 = split[0].ToString();
string temp2 =split[1].ToString().ToString();
string temp3 = split[2].ToString().ToString();
cmd.Parameters.Add("@Vehiclenum", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = split[0];
cmd.Parameters.Add("@sim", SqlDbType.VarChar).Value = split[2];
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException e)
{
Console.WriteLine(e.Message);
err += int.Parse(count.ToString()).ToString() + "#";
}
cn.Close();
strTemp = sr.ReadLine();
}
sr.Close();
err = err;
return count;
}
}
}
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;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.label1.Text = loadData().ToString();
}
public string randStr(int count)
{
string allchar = "1,2,3,4,5,6,7,8,9,0";
string[] allchararray = allchar.Split(',');
string randomcode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < count; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(10);
if (temp == t)
{
return randStr(count);
}
temp = t;
randomcode += allchararray[t];
}
return randomcode;
}
public int loadData()
{
StreamReader sr = new StreamReader(new FileStream("data.txt", FileMode.Open, FileAccess.Read), System.Text.Encoding.Default);
SqlConnection cn = new SqlConnection("Server=.;uid=sa;pwd=1122;database=vehicle"); ;
string insertCmd = "insert into infovehicle(Vehiclenum,vehicleid,sim) values (@Vehiclenum,@id, @sim)";
char delimChar = '\t';
string[] split = null;
int count = 0;
string strTemp = sr.ReadLine();
string strName=null;
string err="";
while (strTemp != null)
{
split = strTemp.Split(delimChar);
SqlCommand cmd = new SqlCommand(insertCmd, cn);
count++;
strName ="V"+ DateTime.Now.ToString("yyyyMMddhhmmss").ToString()+randStr(4);
string temp1 = split[0].ToString();
string temp2 =split[1].ToString().ToString();
string temp3 = split[2].ToString().ToString();
cmd.Parameters.Add("@Vehiclenum", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = split[0];
cmd.Parameters.Add("@sim", SqlDbType.VarChar).Value = split[2];
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException e)
{
Console.WriteLine(e.Message);
err += int.Parse(count.ToString()).ToString() + "#";
}
cn.Close();
strTemp = sr.ReadLine();
}
sr.Close();
err = err;
return count;
}
}
}
相关文章推荐
- Sqlserver bcp批量导入、导出数据(2008-05-23 14:48:40| 分类: 数据库技术)
- C#---批量导入数据库
- 在C#应用程序中,利用表值参数过滤重复,批量向数据库导入数据,并且返回重复数据
- [已解决]C#批量高效率导入大数据到数据库[百万级以上]
- C# 读写excel 用于导入数据库 批量导入导出excel
- C#Excel上传批量导入sqlserver
- 【转】C#批量导入数据到sqlserver
- [置顶] 关于C++、C#实现EXCEL数据库批量导入数据库万行以上数据效率问题
- C# BulkCopy方式数据批量导入数据库
- 简单三层使用DataTable向数据库表批量导入数据---向SqlServer一张表中导入数据
- 数据库信息批量导入到对象(c#)
- C#批量保存方法-利用Net SqlBulkCopy 批量导入数据库,速度超快-通用方法源码提供
- 《项目经验》--简单三层使用DataTable向数据库表批量导入数据---向SqlServer一张表中导入数据
- C#连接数据库、读EXCEL表、批量导入数据
- c#几种数据库的大数据批量插入(SqlServer、Oracle、SQLite和MySql)
- 《项目经验》--简单三层使用DataTable向数据库表批量导入数据---向SqlServer多张张表中导入数据
- 神奇的 SqlBulkCopy 高性能批量导入sqlserver
- sqlserver 向visio导入数据库关系图
- 从数据库批量导入数据到eclipse/studio中
- sqlserver 数据导入到 虚拟主机虚拟空间的数据库名称不一致的问题