VB.NET & SQL,将 SELECT 查询结果粘贴到文本框

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

VB.NET & SQL, pasting SELECT query results to a textbox

sqlvb.netselecttextboxwebpage

提问by Fares K. A.

I'm building a client database system for a travel company.

我正在为一家旅游公司建立一个客户数据库系统。

They want to be able to retrieve all of their customer emails with one click, and have it appear on a textbox on the page, where they can copy it off and paste it into Outlook.

他们希望能够一键检索所有客户电子邮件,并将其显示在页面上的文本框中,在那里他们可以将其复制并粘贴到 Outlook 中。

Currently, the textbox is called emailList, and is invisible until the button called emailGet is clicked.

目前,该文本框称为 emailList,在单击名为 emailGet 的按钮之前是不可见的。

However, I have no idea how to make the text appear into the textbox from an SQL query.

但是,我不知道如何使文本从 SQL 查询出现在文本框中。

My SQL query is: SELECT CEmail FROM Clients. That's pretty much it.

我的SQL查询是:SELECT CEmail FROM Clients。差不多就是这样。

In pseudocode, what I'm trying to do is:

在伪代码中,我想做的是:

sqlQuery = "SELECT CEmail FROM Clients"
Execute select query and store results (in a variable? or maybe directly to the textbox?)
emailList.Text = Result of sqlQuery

Thank you! :)

谢谢!:)

回答by rwisch45

Private Sub GetEmailAddresses() 
        Dim sText As String = String.Empty
    Dim sConnString As String = String.Empty 'Put your connection string in here

    Using cn As New OleDb.OleDbConnection(sConnString)
        cn.Open()
        Dim cmd As New OleDb.OleDbCommand("SELECT CEmail FROM Clients", cn)
        Dim r As OleDb.OleDbDataReader = cmd.ExecuteReader()

        If Not r.HasRows Then Exit Sub

        Do While r.Read()
            sText = sText & ";" & r.GetString(0)
        Loop

        cn.Close()
    End Using

    txtboxList.Text = sText
End Sub