c#高手进来解答一下 谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/11 02:47:09
怎样把记事本中的信息(*.txt)中的信息导入SQL2005数据库中
我在窗体中创建了一个butten打开记事本 然后怎样把用逗号分隔开的信息 如(张三,12岁,三年级)这样的数据批量添加到数据库
while ((line = sr.ReadLine()) != null)
{
string[] s = line.Split(new char[] { ',' });

MessageBox.Show("学号:"+s[0] +" 学生姓名:" + s[1] + " 学科:"+s[2]+"得分:"+s[3]+ "班级"+s[4]);
下面应该怎么做 讲细点 最好有详细代码 谢谢
strParam 是什么意思阿 怎样先判断数据库是否存在该信息
如果存在则提示用户,否则将信息写入数据库

foreach(string str in s)
{
string strParam = str +","
}
strParam.SubString(0,strParam.Length-1);
string sql = "insert into TABLE values("+strParam+")";
SqlConnection cn = new SqlConnection("写你的连接字符串")
SqlCommand cmd = new SqlCommand(sql,cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();

你已经将记事本上的数据分开了,即已经得到了 string[] s;
如果你的数据库表中的字段和你上面的数组数据匹配时,就好操作了;

先获取字符串,想楼上那样:
string strParam = "";
foreach(string str in s)
{
strParam = str + ",";
}

然后就是开打数据库,插入数据,关闭数据;
楼上的已经将代码写出;