如何使用 VB.NET 从 MySQL 数据库查询数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14508843/
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 12:01:22  来源:igfitidea点击:

How do I query data from a MySQL Database using VB.NET

mysqlvb.net

提问by Kraxed

I have a MySQL Database called "business elements", which has 4 columns in it (ID,Username,Password & Level). I want to check if a specific username's (given by Usernametextbox.text) "Level" is Admin, Manager or User. These are the 3 values that I want all of my users to have(They are all in the column Level). My connectionstring is "server=localhost;user id=root;password=;database=business elements"and my table is users

我有一个名为“业务元素”的 MySQL 数据库,其中有 4 列(ID、用户名、密码和级别)。我想检查一个特定的用户名(由 给出Usernametextbox.text)“级别”是管理员、经理还是用户。这些是我希望所有用户都拥有的 3 个值(它们都在列级别中)。我的连接字符串是"server=localhost;user id=root;password=;database=business elements",我的表是users

Basically I want to check the value of a column with a given user.

基本上我想检查给定用户的列的值。

All of this is in Visual Basic.

所有这些都在 Visual Basic 中。

Someone please Help..

有人请帮助..

回答by xpda

Here's how to get the level for a particular username. It should a good starting point for lots of other functions. I haven't tested it, but it should be pretty close. (Userlevel should match the data type for "level"in the table.)

以下是获取特定用户名的级别的方法。它应该是许多其他功能的良好起点。我还没有测试过,但它应该非常接近。(用户级别应与表中“级别”的数据类型匹配。)

Dim conn as MySqlConnection
Dim cmd As MySqlCommand

conn = New MySqlConnection("server=localhost;user id=root;password=;database=business elements;")
conn.Open()

cmd = New MySqlCommand("SELECT level FROM users WHERE Username=@username LIMIT 1", conn)
cmd.Parameters.AddWithValue("@username", Usernametextbox.Text)
userLevel = cmd.ExecuteScalar()