本程序只要是按行读取txt文件内容,并按照一定格式组成SQL语句,输出到debug目录下的liwujun.txt。只要复制到数据里执行,便可以往表里插入内容了。。
注:SQL语句里面的insert into不能一次性插入超过一千条内容。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;
namespace ConsoleApp15
{
class Program
{
[STAThread]
static void Main(string[] args)
{
OpenFileDialog file = new OpenFileDialog();
file.CheckFileExists = true; //检查文件
file.Filter = "|*.txt";
if (file.ShowDialog() != DialogResult.OK)
{
return;
}
string filePath = Path.GetFullPath(file.FileName);
string[] lines = File.ReadAllLines(filePath, System.Text.Encoding.Default);
List<string> list = new List<string>();
string save_path = Application.StartupPath + "\\" + "liwujun.txt";
foreach (var line in lines)
{
string[] arr = new string[3];
arr[0] = line.Split(',')[0];
arr[1] = line.Split(',')[1];
arr[2] = line.Split(',')[2];
string New_line = string.Format("insert into banksichuaninfo(WDCode,WDName,QXName) Values ('" + arr[0]+ "','"+arr[1]+"','"+arr[2]+"')");
list.Add(New_line);
// sw.WriteLine(New_line+"\n");//开始写入值
WriteTxt(New_line, save_path);
}
//sw.Close();
//fs1.Close();
return;
}
/// <summary>
/// 写入记事本
/// </summary>
/// <param name="log">日志内容</param>
/// <param name="filepath">文件路径(含文件名)</param>
/// <returns></returns>
private static bool WriteTxt(string log, string filepath)
{
try
{
string folder = filepath.Substring(0, filepath.LastIndexOf('\\'));
// 创建目录
if (Directory.Exists(folder) == false)
{
Directory.CreateDirectory(folder);
}
// 当文件不存在时创建文件
if (File.Exists(filepath) == false)
{
FileStream fs = File.Create(filepath);
fs.Close();
}
// 写入文件内容
File.AppendAllText(filepath, log + "\r\n", Encoding.Default);
return true;
}
catch
{
return false;
}
}
}
}
有疑问可联系: QQ:1028559784