如何使用 VB.net 在 MS Access 中获取特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22084726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:55:55  来源:igfitidea点击:

How to get specific value in MS Access using VB.net

vb.netms-access

提问by user3363039

I'm trying to get the specific value in my Ms Access Database.. Example. Here is my table in Ms Database..

我正在尝试获取 Ms Access 数据库中的特定值.. 示例。这是我在 Ms 数据库中的表..

I have 3 Columns, that is Name,Age and Telelphone number. Here are the values ..

我有 3 列,即姓名、年龄和电话号码。这里是值..

Row 1 = [Name = John ,Age =13,Telephone Num = 456)].

第 1 行 = [姓名 = 约翰,年龄 =13,电话号码 = 456)]。

Row 2 = [Name = Mark, Age =11,telephone num = 123)].

第 2 行 = [姓名 = 马克,年龄 =11,电话号码 = 123)]。

Row 3 = [Name = Maye, Age =15,telephone num = 789)]

第 3 行 = [姓名 = Maye,年龄 =15,电话号码 = 789)]

then Example I only want to get the Age of Mark and that is 11 or John's telephone number and that is 456..and display it using textbox. All I want is to get specific value and I don't care about the other value..

然后示例我只想获得马克时代,即 11 或约翰的电话号码,即 456 ..并使用文本框显示它。我想要的只是获得特定的价值,我不在乎其他价值。

Well im Using Visual Studio 2012 and MS Access 2010..

好吧,我正在使用 Visual Studio 2012 和 MS Access 2010 ..

heres my code.. Im new about VB.net so that im not relly familiar about the codes..^_^

继承人我的代码.. 我对 VB.net 是新手,所以我对代码不太熟悉..^_^

con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Vincelouie\Desktop\FBES INFO SYSTEM\Database1.accdb") con.Open()

con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Vincelouie\Desktop\FBES INFO SYSTEM\Database1.accdb") con.Open()

    Dim selectString As String = "SELECT  Age FROM Table1"
    ' this line passes in the SQL statement and the OleDbConnection object.
    Dim cmd As OleDbCommand = New OleDbCommand(selectString, con)

    'Send the CommandText to the connection, and then build an OleDbDataReader.

    Dim reader As OleDbDataReader = cmd.ExecuteReader()

    reader.Read()
    StudentTextBox.Text = reader.GetValue(0).ToString()


    'Close the reader and the related connection.
    reader.Close()
    con.Close()

Thank you.. All help are really appreciated..

谢谢.. 所有的帮助都非常感谢..

回答by jmcilhinney

I'm not going to teach you all about ADO.NET here. If you want to learn the basics of data access in VB.NET then there are plenty of places that you can learn that. Here are some code examples:

我不会在这里教你所有关于 ADO.NET 的知识。如果您想学习 VB.NET 中数据访问的基础知识,那么有很多地方可以学习。下面是一些代码示例:

http://www.vbforums.com/showthread.php?469872-Retrieving-and-Saving-Data-in-Databases

http://www.vbforums.com/showthread.php?469872-Retrieving-and-Saving-Data-in-Databases

Addressing this question specifically, you will create an OleDbCommand containing an appropriate SQL query and call ExecuteScalar. That thread I linked to provides an example.

针对这个问题,您将创建一个包含适当 SQL 查询的 OleDbCommand 并调用 ExecuteScalar。我链接到的那个线程提供了一个例子。