如何在VB中显示来自SQL Server的查询结果?

How to display query results from SQL server in VB?(如何在VB中显示来自SQL Server的查询结果?)
本文介绍了如何在VB中显示来自SQL Server的查询结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 VB 中显示来自 SQL 服务器的查询结果.我写了以下代码,但没有得到如何只显示结果";

I am trying to display query results from SQL server in VB. I wrote following code, but not getting how to "Just display the results";

 Public Function ConnectToSQL() As String
        Dim con As New SqlConnection
Try
            con.ConnectionString = "Data Source=(local);Initial Catalog=TestDatabase;Persist Security Info=True;User ID=sa;Password=afm"
            Dim cmd As New SqlCommand("SELECT username FROM users WHERE username='user'", con)
            con.Open()
            Console.WriteLine("Connection Opened")

            ' Execute Query
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            While reader.Read()
                Console.WriteLine(String.Format("{0}, {1}", _
                   reader(0), reader(1)))

            End While
        Catch ex As Exception
            MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
        Finally
            con.Close() 'Whether there is error or not. Close the connection.
        End Try
        Return reader
    End Function

任何人都可以指导吗?谢谢

Can any one guide? Thank you

推荐答案

我纠正了一些问题,现在你的函数对我来说很好:

I corrected a few things and now your function works fine for me:

Public Function ConnectToSQL() As String
    Dim con As New SqlConnection
    Dim reader As SqlDataReader
    Try
        con.ConnectionString = "Data Source=(local);Initial Catalog=TestDatabase;Persist Security Info=True;User ID=sa;Password=afm"
        Dim cmd As New SqlCommand("SELECT username, WindowsLogin FROM users WHERE username='user'", con)
        con.Open()
        Console.WriteLine("Connection Opened")

        ' Execute Query    '
        reader = cmd.ExecuteReader()
        While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
               reader(0), reader(1)))
            'NOTE: (^^) You are trying to read 2 columns here, but you only        '
            '   SELECT-ed one (username) originally...                             '
            ' , Also, you can call the columns by name(string), not just by number '

        End While
    Catch ex As Exception
        MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
    Finally
        con.Close() 'Whether there is error or not. Close the connection.    '
    End Try
    'Return reader { note: reader is not valid after it is closed }          '
    Return "done"
End Function

如果您有任何问题,请告诉我.

Let me know if you have any questions.

这篇关于如何在VB中显示来自SQL Server的查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)