在导入txt文件时,判断文件数据有多少列,然后根据列头信息动态拼接数据库脚本来创建数据表,然后就可以导数据了!思路应该就这样了吧
解决方案 »
- 在winform程序里, 怎么设置TextBox里 部分文字 的大小
- C#关于ToolTip闪烁问题
- 怎么用程序模拟打开一个链接
- 我现在明白了,C#为什么容易使用,C#就提供了大量的资料和中文资料.为什么Delphi在中国价格很贵,没有中文版,资料也少,提高了学习难度?
- 创建的线程阻塞,造成主界面也卡死, 高分寻解
- 我想在其他程序上增加一个连接页面,全部代码如下,但是现实出来的是一个空白页
- VS2005集成环境开发遇到的问题
- 有没有人用过Sniffer这个软件,用C#能做出这种软件吗?
- C#中写web程序如何排版?
- 新手提问:关于DataGrid
- 多线程从远程数据库查询数据问题
- 新人求关注,分享个vs插件
数据全放在txt文件里,我确实刚开始学这个,你说的对我难度太大了,我要写的程度就是打开一个TXT文件导入数据
类似这样:
public void insertdata(string s1, string s2, string s3,string s4,string s5)//插入数据库
{
SqlConnection con = new SqlConnection("Data Source=10.168.1.5;Initial Catalog=data;User ID=sa;password=sa;Integrated Security=False");
con.Open();
SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from newtable where 序号= '{0}'", s1), con);
if ((int)cmd.ExecuteScalar() > 0)
{
listBox1.Items.Add(s1 + " 数据已经存在");
}
else
{
string sql = "insert into newtable(序号,时间,编号,类型,操作员编号) values('" + s1 + "','" + s2 + "','" + s3 + "','" + s4 + "','" + s5+ "')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
listBox1.Items.Add(s1 + " 成功添加");
}
cmd.Dispose();
con.Close();
} private void button5_Click(object sender, EventArgs e)
{
using (StreamReader sr = new StreamReader("test.txt", Encoding.Default))//系统读写文件方法
{
string s = "";
while ((s = sr.ReadLine()) != null)
{ string[] news = s.Split(' ,');//01, 201403281834,11222333331,03, 20100
insertdata(news[0], news[1], news[2], news[3], news[4]);
}
sr.Close(); }
}