C# 将txt文件的数据库元组导入数据库,代码实现
2017-05-07 18:22
489 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.IO; namespace 导入数据 { class Program { static void Main(string[] args) { string str = "Data Source=LENOVO-PC;Initial Catalog=MyDatabase;Integrated Security=True"; int n = -1;//判断是否导入成功 using (StreamReader sr=new StreamReader("1.txt")) { string line = sr.ReadLine();//第一行列名不要 using (SqlConnection sco = new SqlConnection(str)) { string sql = "insert into users1(logName, logPassWord) values(@logName,@logPassWord)"; //用参数代替拼接,防止SQL注入漏洞 SqlParameter[] ps = { new SqlParameter("@logName",System.Data.SqlDbType.VarChar), new SqlParameter("@logPassWord",System.Data.SqlDbType.VarChar) }; sco.Open(); using (SqlCommand scd = new SqlCommand(sql, sco)) { scd.Parameters.AddRange(ps); while ((line=sr.ReadLine())!=null) { string[] texts = line.Split(new char[] { ','},StringSplitOptions.RemoveEmptyEntries); ps[0].Value = texts[0]; ps[1].Value = texts[1]; n = scd.ExecuteNonQuery(); } } sco.Close(); } } if (n > 0) { Console.WriteLine("导入成功!"); } else { Console.WriteLine("导入失败!"); } Console.ReadKey(); } } }
相关文章推荐
- C#将数据库的元组导出生产txt文件,代码实现
- c#将Excel数据导入到数据库的实现代码
- C# TXT文件导入至数据库
- C# 文件上传下载(Excel导入,多线程下载)功能的实现代码
- c#将Excel数据导入到数据库的实现代码
- C#读取TXT文件的代码实现
- C#读取TXT文件的代码实现
- C#备份及还原数据库的实现代码(粗略) // 利用C#还原数据库(SQL SERVER)备份文件到指定路径
- c#读取txt文件并导入到数据库
- c#将Excel数据导入到数据库的实现代码
- c#将Excel数据导入到数据库的实现代码(OleDb)
- C# TXT文件导入至数据库
- c#将Excel数据导入到数据库的实现代码(转载)
- c#将Excel数据导入到数据库的实现代码
- 把数据从txt文本文件导入到数据库的实现方法
- 用java实现从txt文本文件批量导入数据至数据库
- VC 实现 txt文件 导入数据库
- 采用struts2+hibernate实现保存文件到Oracle数据库&&并附下载代码【导入即可用】;数据库中保存blob类型的二进制文件
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库.txt
- C#实现数据库事务处理的简单示例代码