从Text文本中读值插入到数据库中

来源:互联网 发布:关于远离网络的演讲稿 编辑:程序博客网 时间:2024/06/06 01:22

/// <summary>
  /// 转换数据,从Text文本中导入到数据库中
  /// </summary>
  private void ChangeTextToDb()
  {
   if(File.Exists("Storage Card/Zyk.txt"))
   {
    try
    {
     this.RecNum.Visible=true;
     SqlCeCommand sqlCreateTable = Mc.gConn.CreateCommand();  
     StreamReader reader = new StreamReader("Storage Card/Zyk.txt", System.Text.Encoding.Default);
     int i=0;
     while (reader.Peek() > 0)
     {
    
      string oneLine = reader.ReadLine();
      string ztm = this.MySubstring(oneLine,0,100).Trim().Replace("'","’");
      string date = this.MySubstring(oneLine,100,8).Trim().Replace("'","’");
      string isbn = this.MySubstring(oneLine,108,20).Trim().Replace("'","’");
      string fbs  = this.MySubstring(oneLine,128,10).Trim().Replace("'","’");

      if(fbs=="0"||fbs==null||fbs==""||isbn==""||isbn==null)
      {      
      }
      else
      {
      
       isbn=NcIsbn.Format(isbn,13).Replace("-","");

       sqlCreateTable.CommandText = "insert into ZYK(Title,RDate,ISBN,BookNum) values('"+ztm +"','"+date+"','"+isbn+"',"+ fbs+")";
       sqlCreateTable.ExecuteNonQuery();
       i+=1;
       if(i%100==0)
       {
        this.RecNum.Text=i.ToString();
        this.RecNum.Refresh();
       }
      }
      
     }
     reader.Close();
     MessageBox.Show("馆藏数据成功导入"+i.ToString().Trim()+"条!");
     this.RecNum.Visible=false;
    }
    catch
    {
     MessageBox.Show("馆藏数据导入失败请检查格式!");
     this.RecNum.Visible=false;
    }
   }
   else
   {
    MessageBox.Show("没有Zyk.txt文件!");
   }
  
   
  } 

原创粉丝点击