简体   繁体   English

VB.NET中的WHERE语句

[英]WHERE Statement in VB.NET

For a school assignment I need to name computer based on their MAC-Adress. 对于学校作业,我需要根据其MAC地址为计算机命名。 I have a mySQL table with two columns Mac-Adress and Computername. 我有一个包含两列Mac-Adress和Computername的mySQL表。 I want to use the computer's mac adress via following code, which works. 我想通过以下代码使用计算机的mac地址,它可以正常工作。

Declaration of the globals 全球宣言

Public connStr As String = "Database=Mac;" Public connStr As String =“数据库= Mac;” & _ "Data Source=localhost;" &_“ Data Source = localhost;” & _ "User Id=root;Password=" Public mac_adress As String &_“ User Id = root; Password =” Public mac_adress As String

Getting the Mac Adress 获取Mac地址

Public Sub MacAdres() Dim nic As NetworkInterface = Nothing Public Sub MacAdres()Dim nic as NetworkInterface =否

  For Each nic In NetworkInterface.GetAllNetworkInterfaces mac_adress = nic.GetPhysicalAddress.ToString Next End Sub 

However, when I want to create the WHERE statement, I get an error. 但是,当我要创建WHERE语句时,出现错误。 I'm using the following code for the WHERE statement. 我在WHERE语句中使用以下代码。

 Public Sub retriveData() Try Dim query As String = "SELECT * FROM 'mactable' WHERE 'Mac-adres' = '" & mac_adress & "'" Dim connection As New MySqlConnection(connStr) Dim cmd As New MySqlCommand(query, connection) connection.Open() Dim reader As MySqlDataReader reader = cmd.ExecuteReader() While reader.Read() MessageBox.Show(reader.GetString(1)) End While reader.Close() connection.Close() Catch ex As Exception Console.WriteLine(ex.Message) End Try End Sub 

What am I doing wrong? 我究竟做错了什么?

A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in >MySql.Data.dll > MySql.Data.dll发生类型为'MySql.Data.MySqlClient.MySqlException'的第一次机会异常

我认为您不需要表名或字段名上的引号,并且可以删除连字符。

Dim query As String = "SELECT * FROM mactable WHERE MacAdres = '" & mac_adress & "'"

Solved it by using these: "`" instead of these '. 通过使用以下命令解决了此问题:“`”而不是这些'。 Thats rather peculiar, but it solved it. 那是相当奇怪的,但是它解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM