数据读取器将变量设置为标签

数据读取器将变量设置为标签

本文介绍了使用 SQL 数据读取器将变量设置为标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很长时间来解决这个问题,但似乎无法解决问题.我使用的是 MySql 和 Visual Basic.

I have spent so long on figuring this out however cant seem to resolve the problem. I am using MySql and Visual Basic.

我正在使用 select 语句来获取我需要的变量,然后使用数据读取器进行循环,获取变量(到字符串),最后将它们设置为我在表单中使用的文本标签.我确信我正在遵循正确的程序,但这似乎并没有带来结果.

I am using the select statement to get the variables i need, then using data reader to loop through, get the variables (to string) and finally set these to text labels which I am using in a form. I am sure I am following the correct procedure for this, but this does not seem to bring back results.

如果有人能帮助我,我将不胜感激.我的代码如下;

If anybody could help me i would be very much appreciated. The code I have is below;

    lblName.Text = Form1.topicName

    MyConnString = "Database=case management system;Data Source=localhost;User Id=root;Password="abc"
    Dim MySqlConn As New MySqlConnection(MyConnString)
    Dim MySelectQuery As String = "SELECT topic_id, topic_name, description, useful_links, employee_id, date "
       & "FROM knowledge_base "
       & "WHERE topic_name = '" & lblName.Text & "'"
    Dim myCommand As New MySqlCommand(MySelectQuery)
    myCommand.Connection = MySqlConn
    MySqlConn.Open()
    myCommand.ExecuteNonQuery()
    mydata = myCommand.ExecuteReader
    While mydata.Read()
        labelDescription.Text = mydata.Item("description").ToString
        labelLinks.Text = mydata.Item("useful_links").ToString
    End While
    mydata.Close()
    myCommand.Connection.Close()
    MySqlConn.Close()

推荐答案

删除包含 myCommand.ExecuteNonQuery() 的行.你在做查询!

Remove the line containing myCommand.ExecuteNonQuery(). You are doing query!

此外,标签(说明和链接)将仅包含返回的最后一行的最后数据.

Also, the labels (descriptions and links) will only contain the last data from the last row returned.

将文本输入直接添加到 SQL 命令允许 SQL 注入攻击.您应该使用 sql 参数.请参阅 http://www.dotnetperls.com/sqlparameter

Adding text input directly to a SQL command allows a SQL injection attack. You should use sql parameters. See http://www.dotnetperls.com/sqlparameter

这篇关于使用 SQL 数据读取器将变量设置为标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:30