C# 将文本文件中的数据插入SQL Server表中
我有一个大的文本文件,即Samples.txt文件,每8行是一行,要插入sql server中的一个表中,并且在提到的文本文件中数据的格式如下: 公司名称:Xpress Care 部门:运输和储存 经营类型:物流服务 许可证号码:D-39277 有效期:2012-07-18 联系电话:0771709155/0789444211 电邮:naikmalemail@hotmail.com 地址:第4区塔曼第4街119号 到目前为止,我编写了以下代码,试图将其转换为一种格式,以便像下面这样插入到表中C# 将文本文件中的数据插入SQL Server表中,c#,sql-server,file-io,C#,Sql Server,File Io,我有一个大的文本文件,即Samples.txt文件,每8行是一行,要插入sql server中的一个表中,并且在提到的文本文件中数据的格式如下: 公司名称:Xpress Care 部门:运输和储存 经营类型:物流服务 许可证号码:D-39277 有效期:2012-07-18 联系电话:0771709155/0789444211 电邮:naikmalemail@hotmail.com 地址:第4区塔曼第4街119号 到目前为止,我编写了以下代码,试图将其转换为一种格式,以便像下面这样插入到表中 i
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();
}
我试图对代码做的是,我想将键值对添加并保留到哈希表中,共8行,这样我就可以使用8个值插入if(linenumber==8)
条件部分表中的8列中,但现在它只保留最后一行的值
我将非常感谢你的帮助和想法。如果您在理解问题时遇到困难,请允许我详细解释,或者是否有其他方法可以解决此问题。似乎您需要将哈希表的声明和初始化移到循环之外,并在完成八行块读取后将其清除
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++;
}
这其中的一部分。我建议你用不同的课程来学习。在您的情况下,我将使用一种强类型方法来解决问题您必须首先创建包含所有目标字段的表。然后
LOAD DATA INFILE '../sample.txt' INTO TABLE PerformanceReport;
默认情况下,加载数据填充使用制表符分隔,每行一行,因此应该很好地接受它。如果TXT文件中的格式始终相同,为什么不使用此格式
`而((line=file.ReadLine())!=null)
{
控制台写入线(行);
//使用:delimeter拆分
string[]value=line.Split(“:”);
如果(值[0]=“公司名称”)公司=值[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
}`
如果是一个大文件,您可以将数据存储在DataTable(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);
}
System.IO.StreamReader file=new System.IO.StreamReader(@“c:\sample.txt”);
字符串行=null;
int linecounter=0;
//结构来保存要写入数据库的数据
System.Data.DataTable table=新的System.Data.DataTable();
表.栏.添加(“公司”);
表.列.添加(“部门”);
表.列.添加(“操作”);
表.列.添加(“许可证”);
表.列.添加(“到期日”);
表.列.添加(“联系人”);
表.列.添加(“电子邮件”);
表.列.添加(“地址”);
System.Data.DataRow行=空;
而((line=file.ReadLine())!=null)
{
//如果行为{0,8,16,…},则创建一个新表行
如果(行计数器%8==0)
行=table.NewRow();
string[]value=line.Split(“:”);
//根据“linecounter%8”将数据放入相应的列中
行[行计数器%8]=值[1];
//如果该行已完全填充,则将其添加到表中
如果(行计数器%8==7)
table.Rows.Add(行);
linecounter++;
}
file.Close();
字符串连接字符串=”;
使用(System.Data.SqlClient.SqlBulkCopy copy=new System.Data.SqlClient.SqlBulkCopy(connectionString))
{
copy.DestinationTableName=“MyTable”;
copy.WriteToServer(表);
}
您可以在以下位置获得创建连接字符串的帮助:
注意:此方法假设您已经在SQL Server中创建了一个名为“MyTable”的表,并且它在DataTable中指定了8个varchar列。非常感谢。非常感谢。非常感谢。非常感谢,非常感谢。