用C#写一段代码把txt文本导入到sql数据库指定表里面 txt文本是以空格隔开,表已经建好
txt格式如下如:201104259:3026751000003201104259:3124751000003201104259:32267510000420110425...
txt格式如下如:
20110425 9:30 26 75 100000 3
20110425 9:31 24 75 100000 3
20110425 9:32 26 75 10000 4
20110425 9:33 26 75 100000 3
20110425 9:34 26 75 100000 6
20110425 9:35 26 75 10000 7
20110425 9:36 27 75 100000 3
每一行数据之间是一个空格;
表结构就是
id table1 table2 table3 table4 table5 table6 展开
20110425 9:30 26 75 100000 3
20110425 9:31 24 75 100000 3
20110425 9:32 26 75 10000 4
20110425 9:33 26 75 100000 3
20110425 9:34 26 75 100000 6
20110425 9:35 26 75 10000 7
20110425 9:36 27 75 100000 3
每一行数据之间是一个空格;
表结构就是
id table1 table2 table3 table4 table5 table6 展开
3个回答
展开全部
首先通过循环读取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);
}
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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
{
// 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);
}
更多追问追答
追问
麻烦给我完整的程序代码吧 把程序打包发给我吧 jyteam@qq.com 谢谢啦
追答
上面的就可以,你自己修改一下就可以了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-04-19
展开全部
没表的结构信息啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询