在表格中插入数据到txt文件 [英] insert data in table frm a txt fle
问题描述
我正在用C#创建一个Windows窗体应用程序。我创建了一个名为(h-index)的表。现在我必须通过读取记事本文件中的数据来在表中添加数据。你能帮我解决这个问题?????
你好,
请参考以下代码。
1)首先,逐行阅读文件并保存在列表中。
< pre lang =xml>列表< string > ; myValues = new List < string > 跨度>();
字符串行;
//读取文件并逐行显示。
System.IO.StreamReader file =
new System.IO.StreamReader(c:\\test.txt);
while((line = file.ReadLine())!= null)
{
myValues.Add(line);
}
2)然后通过OleDB打开DB到数据库的连接,并通过一个数据库将值插入数据库插入声明。
private void InsertMyValue( string myValue){
dbconnection.Open();
string setValues = INSERT INTO YourTable( myColumn)VALUES(' + myValue + ');;
OleDbCommand cmd = new OleDbCommand(setValues,dbconnection);
cmd.ExecuteNonQuery();
dbconnection.Close();
}
3)然后在foreach中调用方法 - 子句
foreach(字符串myLine in myValues){//浏览List with all 行
dbconnection.InsertMyValue(myLine); //获取中的每个项目列表和调用Insert-Method
}
您可以参考链接 http://stackoverflow.com/questions/14922360/insert-data-from-a-text-file-into-a-table-in-c-尖锐的
谢谢,
Bh @ gyesh
如果文本文件是分隔文件或固定列长度文件,您可以连接到它 OleDb
连接并通过查询来读取数据。
OleDbCommand cmd = new OleDbCommand(,new OleDbConnection(在这里替换你的连接字符串));
cmd.Connection.Open();
System.IO.File.ReadAllLines(@C:\ testfiles\testfile.txt)。ToList < string > ()。ForEach(delegate(string InsertingValue)
{
cmd.CommandText =Insert into YourTable(ColumnName)VALUES('+ InsertingValue +');;
cmd.ExecuteNonQuery();
});
用文件路径替换文件路径
I am creating a windows form application in C#. I have created a table with the name of(h-index).and now I have to add data in the table by reading the data from a notepad file. Can you help me in this problem ?????
Hi,
Please refer following code.
1) First, read the file line by line and save in list.
List<string> myValues = new List<string>(); string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt"); while((line = file.ReadLine()) != null) { myValues.Add(line); }
2) Then open a DB-Connection to your DB via OleDB and insert the values into your database via an INSERT INTO Statement.
private void InsertMyValue(string myValue){ dbconnection.Open(); string setValues = "INSERT INTO YourTable(myColumn) VALUES ('" + myValue+ "');"; OleDbCommand cmd = new OleDbCommand(setValues, dbconnection); cmd.ExecuteNonQuery(); dbconnection.Close(); }
3) Then call the method in a foreach - clause
foreach(string myLine in myValues){ //Go through the List with all the Lines dbconnection.InsertMyValue(myLine); //Get every item in the List and call the Insert-Method }
You can refer linkhttp://stackoverflow.com/questions/14922360/insert-data-from-a-text-file-into-a-table-in-c-sharp
Thanks,
Bh@gyesh
If the text file is a delimited file or a fixed column length file, you can connect to it OleDb
connection and read data by querying it.
OleDbCommand cmd = new OleDbCommand("",new OleDbConnection("Replace Your Connection string here")); cmd.Connection.Open(); System.IO.File.ReadAllLines(@"C:\testfiles\testfile.txt").ToList<string>().ForEach(delegate(string InsertingValue) { cmd.CommandText = "Insert into YourTable(ColumnName) VALUES ('" + InsertingValue + "');"; cmd.ExecuteNonQuery(); });
Replace The file path with your file path
这篇关于在表格中插入数据到txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!