txt文件将c#导入sql吗? [英] txt file import c# to sql?

查看:97
本文介绍了txt文件将c#导入sql吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void TextDateYukle()
{
  using (StreamReader sr = new StreamReader(File.Open("C:\\Tr500.txt", FileMode.Open)))
  {
    using (SqlConnection txtbaglan = new SqlConnection("server=.;database=.......;trusted_connection=true"))
    {
      txtbaglan.Open();
      string line = "";
      while ((line = sr.ReadLine()) != "")
      {
        string[] parts = line.Split(new string[] { "," }, StringSplitOptions.None);
        string cmdTxt = string.Format("INSERT INTO pdks(cihazno,cardno,shift,tarih,saat) VALUES ('{0}','{1}','{2}','{3}','{4}')", parts[0], parts[1], parts[2], parts[3], parts[4]);//", parts[0], parts[1]);
        using (SqlCommand cmddd = new SqlCommand(cmdTxt, txtbaglan))
        {
          cmddd.ExecuteNonQuery();
        }
      }
    }
  }
}




string []个部分= line.Split(新的string [] {&,&"},StringSplitOptions.None);

未处理空引用说明
对象引用未设置为对象的实例.


此行中的错误,Visual Studio不会给出任何错误消息或警告,您为什么认为可能是问题所在?

好工作




string[] parts = line.Split(new string[] { "," }, StringSplitOptions.None);

Null Reference Excaption was unhandled
Object reference not set to an instance of an object.


error in this line, Visual Studio does not give any error message or a warning, Why do you think the problem may be???

good work

推荐答案

快来吧!

我已经在这里回答了您类似的问题,即使不是几乎相同的问题:

http://www.codeproject.com/Questions/132988/将文本文件导入SQL-cell-problem.aspx

我以为你会从中学到一些东西.

好吧,答案 很有可能 可能是这样的:文本文件中的分隔符不是最后一次的逗号.可能是分号或其他内容.

查看您要处理的文本文件,您将看到.

修改:
原因可能是:行为空,因为您正尝试读取直到行等于".如果文件中没有空行,则很可能会发生这种情况.
Come on mhr!

I already answered your similar if not almost identical question here:

http://www.codeproject.com/Questions/132988/Text-file-to-SQL-import-cells-problem.aspx

I thought you''d learn something from that.

Well, the answer most probably might be this: The delimeter in your text file is not a comma as the last time. It might be semicolon or what ever.

Look into the textfile you''re trying to process and you''ll see.

Modification:
This is more likely the cause: Line is null since you''re trying to read until line is equal to "". If there isn''t an empty line in your file this may very well happen.
while(!String.IsNullOrEmtpy(line = sr.ReadLine()))
{
...
...



最好检查行是否为空或为null,因为如果您尝试执行一行,则会得到null引用异常.当line实际上为null时,请分割(...).

修改结束


垂头丧气,


曼弗雷德(Manfred)



better check for line being either empty or null, as you''ll get a null reference exception if you try to do a line.Split(...) when line is actually null.

End of modification


Crestfallen,


Manfred


尝试使用以下代替方法:
Try using this instead:
while (!string.IsNullOrEmpty(line = sr.ReadLine()))
   {
   string[] parts = line.Split(',');
   ...


优秀的网站用户和会员,非常感谢..

我解决了问题,但是找到了当前的erich ...

谢谢ManfredRBihy !!!
Excellent site users and members have an excellent, thank you..

I solved the problem, but I find the current erich ...

Thank you ManfredRBihy!!!


这篇关于txt文件将c#导入sql吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆