简体   繁体   English

vb.net 中 mysql 数据库的 For Each 循环

[英]For Each loop for mysql database in vb.net

I'm trying to create a reminder that notifies users the number of days before a chemical expires.我正在尝试创建一个提醒,通知用户化学品过期前的天数。 I'm trying to retrieve the data in my database using ForEach loop, but I can't get the syntax right.我正在尝试使用ForEach循环检索数据库中的数据,但语法不正确。

For Each chemicalName as DataRow In chemicalinventory.Rows

For some reason, the error: name ' chemicalinventory ' not declared keeps popping up.出于某种原因,错误:名称“ chemicalinventory库存”未声明不断弹出。

From what I seen online, that is supposed to be the table name.从我在网上看到的,应该是表名。 Am I coding it wrong?我编码错了吗? Please answer if you know what's the error.如果您知道错误是什么,请回答。

You cannot just directly start querying tables in this manner.您不能以这种方式直接开始查询表。 You need to select your data from the database table and use it to fill a DataTable .您需要从数据库表中选择数据并使用它来填充DataTable You can then loop through the rows of this DataTable to get what you're after.然后,您可以遍历此 DataTable 的行以获取您想要的内容。

There are many ways to do that, but here is a very basic setup - granted, you'd need to specify a connection string in your Web.Config, and modify the SQL query to be relative to your needs.有很多方法可以做到这一点,但这里有一个非常基本的设置 - 当然,您需要在 Web.Config 中指定一个连接字符串,并根据您的需要修改 SQL 查询。

Dim sqlConnection1 As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("YOUR_CONNECTION_STRING").ConnectionString)
Dim cmd As New SqlCommand
cmd.CommandType = Data.CommandType.Text
cmd.CommandText = "SELECT * FROM chemicals"
cmd.Connection = sqlConnection1

Dim DataAdapter As New SqlDataAdapter
DataAdapter.SelectCommand = cmd

Dim chemicalInventory As New DataTable()
DataAdapter.Fill(chemicalInventory)

For each row as DataRow in chemicalInventory.rows
   Response.write(row.item("column_name"))
Next

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

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