我有一个很大的文本文件,即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列,但现在它只保留最后一行的值。
我真的很感谢你的帮助和想法。如果您在理解问题时遇到困难,请让我解释一下更多细节,或者是否有其他方法来解决问题。
非常感谢。非常感谢。 – yousufqureshi