gpt4 book ai didi

c# - 将文本文件中的数据插入表 SQL Server

转载 作者:太空宇宙 更新时间:2023-11-03 15:54:26 28 4
gpt4 key购买 nike

我有一个很大的文本文件,即 Samples.txt 文件,每 8 行是一行要插入到 sql server 中的表中,数据在上述文本文件中的格式如下,

Company Name:Xpress Care

Sector:Transportation and storage

Operation Type:Logistic Services

License Number:D-39277

Expiry Date:2012-07-18

Contact Numbers:0771709155 / 0789444211

Email:naikmalemail@hotmail.com

Address:House 119, Street 4, Taemany, District 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','naikmalemail@hotmail.com','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','naikmalemail@hotmail.com','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 列,但现在它只保留最后一行的值。

非常感谢您的帮助和想法。如果您无法理解问题,请让我解释更多细节,或者是否有其他方法可以做到这一点。

最佳答案

看来您需要将 HashTable 的声明和初始化移到循环之外,并在完成八行 block 读取后将其清除

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++;
}

其中一部分。我建议为您的工作使用不同的类(class)。在您的情况下,我会使用 Dictionary<string,string> 对问题采用强类型方法

关于c# - 将文本文件中的数据插入表 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24230205/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com