2014-06-15 81 views
2

我有一个很大的文本文件,即Samples.txt文件,每8行是一行要插入到sql服务器中的表中,数据的格式如下在提到的文本文件,从文本文件插入数据到一个表SQL Server

公司名称:随心护理

领域:运输和储存

操作类型:物流服务

许可证编号:d-39277

到期日:2012-07-18

联系电话:0771709155/0789444211

电子邮件:[email protected]

地址:府119街4号,Taemany,区4

到目前为止,我编写了下面的代码,试图将它带入一个格式,以便我可以像下面那样插入表格。

insert into table(company, sector, operation, license, expiry, contact, email, address) Values ('Xpress Care','Transportation and storage','Logistic Services','D-39277','2012-07-18', '0771709155/0789444211','[email protected]','House 119, Street 4, Taemany, District 4'); 

这里是我写的代码:

static void Main(string[] args) 
    { 
     int counter = 0; 
     int linecounter = 1; 
     string line; 

     // Read the file and display it line by line. 
     System.IO.StreamReader file = 
     new System.IO.StreamReader("c:\\sample.txt"); 
     while ((line = file.ReadLine()) != null) 
     { 
      Console.WriteLine(line); 
      // splite with the : delimeter 
      string[] values = line.Split(':'); 
      //Console.WriteLine("column name:- {0} value:- {1}",values[0],values[1]); 

      //hashtable to store the key value pairs from the text file 
      Hashtable myHT = new Hashtable(); 

      // I AM STUCK here!!! I want to add to and keep the values for 8 lines 
      myHT.Add(values[0], values[1]); 

//if linecounter is 8 then I have the values for one new row to be inserted in the table 
      if (linecounter == 8) 
      { 
       Console.WriteLine("\n\r code to insert the values in the query example below from the hashtable\n\r"); 

// insert into table(company, sector, operation, license, expiry, contact, email, address) Values ('Xpress Care','Transportation and storage','Logistic Services','D-39277','2012-07-18', '0771709155/0789444211','[email protected]','House 119, Street 4, Taemany, District 4'); 


       // reset the linecounter and empty the hashtable here for the next row to insert 
       linecounter = 0; 
      } 

      linecounter++; 
      counter++; 
     } 

     file.Close(); 

     // Suspend the screen. 
     Console.ReadLine(); 
    } 

我试图用代码做的是,我想补充和保持键值对为HashTable 8行,所以我可以使用8个值插入if(linenumber==8)条件部分的表格中的8列,但现在它只保留最后一行的值。

我真的很感谢你的帮助和想法。如果您在理解问题时遇到困难,请让我解释一下更多细节,或者是否有其他方法来解决问题。

回答

0

看来你需要移动外循环的哈希表的声明和初始化并清除它,当你已经完成了八通线块读取

static void Main(string[] args) 
{ 
    int counter = 0; 
    int linecounter = 1; 
    string line; 

    //hashtable to store the key value pairs from the text file 
    Hashtable myHT = new Hashtable(); 

    // Read the file and display it line by line. 
    System.IO.StreamReader file = 
    new System.IO.StreamReader("c:\\sample.txt"); 
    while ((line = file.ReadLine()) != null) 
    { 
     .... 

     // Continue to add values to the hashtable until you reach the 8 row boundary 
     myHT.Add(values[0], values[1]); 

     if (linecounter == 8) 
     { 
      ..... insert ... 

      // reset the linecounter and empty the hashtable here for the next row to insert 
      linecounter = 0; 
      myHT.Clear(); 
     } 

     linecounter++; 
     counter++; 
    } 

一部分来源于此。我建议为你的工作使用不同的课程。在你情我愿用Dictionary<string,string>有一个强类型方法,你必须创建表与所有first.then

LOAD DATA INFILE '../sample.txt' INTO TABLE PerformanceReport; 

的目标字段默认情况下LOAD DATA INFILE问题

+0

非常感谢。非常感谢。 – yousufqureshi

0

使用制表符分隔,每行一行,所以应该很好。

0

如果在TXT文件格式始终是相同的,为什么不使用这个..

',而((行= file.ReadLine())!= NULL){ Console.WriteLine(行) ; //分割::delimeter string [] values = line。分裂(':'); if(values [0] ==“Company Name”)company = value [1];

 if ((line = file.ReadLine()) != null) 
     string[] values = line.Split(':'); 
     if (values[0]== "Sector") Sector= value[1]; 
      ... 
      ... 
     insert into table(company, sector, operation, license, expiry, contact, email, address) 
     (@company, @sector,..... 
     ///please insure injection protection 
     }` 
0

如果它是一个大的文件,你可以存储你的数据在数据表(System.Data.DataTable),然后迅速使用SqlBulkCopy的(System.Data.SqlClient.SqlBulkCopy)写。该守则将是这个样子:

 System.IO.StreamReader file = new System.IO.StreamReader(@"c:\sample.txt"); 
     string line = null; 
     int linecounter = 0; 

     //structure to hold data to be written to the database 
     System.Data.DataTable table = new System.Data.DataTable(); 
     table.Columns.Add("company"); 
     table.Columns.Add("sector"); 
     table.Columns.Add("operation"); 
     table.Columns.Add("license"); 
     table.Columns.Add("expiry"); 
     table.Columns.Add("contact"); 
     table.Columns.Add("email"); 
     table.Columns.Add("address"); 


     System.Data.DataRow row = null; 
     while ((line = file.ReadLine()) != null) 
     { 
      //create a new table row if the line is {0,8,16,...} 
      if (linecounter % 8 == 0) 
       row = table.NewRow(); 

      string[] values = line.Split(':'); 

      //put the data in the appropriate column based on "linecounter % 8" 
      row[linecounter % 8] = values[1]; 

      //add the row to the table if its been fully populated 
      if (linecounter % 8 == 7) 
       table.Rows.Add(row); 

      linecounter++; 
     } 

     file.Close(); 

     string connectionString = "<CONNECTION STRING GOES HERE>"; 
     using (System.Data.SqlClient.SqlBulkCopy copy = new System.Data.SqlClient.SqlBulkCopy(connectionString)) 
     { 
      copy.DestinationTableName = "MyTable"; 
      copy.WriteToServer(table); 
     } 

你可以得到帮助,在创建您的连接字符串:Connection strings Reference

注意:此方法假定您已经创建了一个名为“MyTable的”在SQL Server表,并且它具有在DataTable中指定的8个varchar列。

+0

非常感谢,真的很感激。 – yousufqureshi

相关问题