如何从ms访问数据库中检索数据? [英] how to retrieve data from ms access database?
问题描述
喜!我想读取由\分隔的文本文件的逐行内容,并与ms访问数据库内容匹配,并在另一个文本文件中显示下一个字段的相应数据。
从第二篇文章移到这里:
示例:
textfile的内容如下:
重音
加速
访问
Ms access数据库包含:
字义
重音音调
加速加速
获取收益
我必须从文本文件中读取重音并在单词字段中匹配重音并在另一个文本文件中显示音调(nextfield的内容)。
hi! I want to read line by line contents of text file delimited by \ and match with ms access database contents and display the corresponding data of next field in another text file.
Moved here from second post:
Example:
textfile has contents such as:
accent
acceleration
access
Ms access Database contains:
word meaning
accent tone
acceleration hastening
access gain
I have to read accent from textfile and match accent in word field and display tone(content of nextfield)in another textfile.
推荐答案
逐行阅读很简单。最简单的方法是将它们全部读入数组:
Reading line by line is simple. The easiest way is to read them all into an array:
string[] lines = File.ReadAllLines(pathToTheFile);
然后你可以使用一个简单的 foreach
循环来处理每一行。
然后,您需要访问循环内的数据库:
You can then use a simple foreach
loop to work through each line.
Then, you need to access the database inside the loop:
using (OleDbConnection con = new OleDbConnection(strConnect))
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand("SELECT Id, description FROM myTable WHERE mySearchColumn = @SEARCH", con))
{
cmd.Parameters.AddWithValue("@SEARCH", mylineFromTheFile);
using (OleDbDataReader reader = cmd.ExecuteQuery())
{
if (reader.Read())
{
int id = (int) reader["Id"];
string desc = (string) reader["description"];
...
}
}
}
}
我建议阅读:从.NET应用程序访问Microsoft Office数据 [ ^ ]
I'd suggest to read this: Accessing Microsoft Office Data from .NET Applications[^]
这篇关于如何从ms访问数据库中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!