用C#写一段代码把txt文本导入到sql数据库指定表里面 txt文本是以空格隔开,表已经建好

2024-11-01 15:29:36
推荐回答(3个)
回答1:

首先通过循环读取txt的内容,读一条写入一条
string id,timestr,id1,id2,id3,valuenum,sql;
While ((line = smRead.ReadLine()) != null) //需重新实例化
{
string[] strList = line.Split('');
id= strList[0].ToString();
timestr = strList[1].ToString();
id1=strList[2].ToString();
id2=strList[3].ToString();
id3=strList[4].ToString();
valuenum=strList[5].ToString();
sql=insert into 表名(v1,v2,v3,v4,v5,v6) values (id,timestr,id1,id2,di3,valuenum);
savedata(sql,conn);
}

回答2:

try
{
// Create an instance of StreamReader to read from a file.

// The using statement also closes the StreamReader.

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of

// the file is reached.
string[] stringSeparators = new string[] { " " };
string[] result;
while ((line = sr.ReadLine()) != null)
{
result = source.Split(stringSeparators, StringSplitOptions.None);
foreach (string s in result)
{
//每个空格分隔的内容,然后你可以写入数据库,自己写吧!
}
//Console.WriteLine(line);

}
}
}
catch (Exception e)
{
// Let the user know what went wrong.

Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}

回答3:

没表的结构信息啊