从 VB.Net 中的 SQL 查询读取数据

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

Read data from SQL Query in VB.Net

sqlsql-servervb.net

提问by Demonicpenguin

I need to gather information from an SQL query. I am using a SQL Data Source control because I will be using it in a grid view after I get the data.

我需要从 SQL 查询中收集信息。我正在使用 SQL 数据源控件,因为我将在获取数据后在网格视图中使用它。

My query looks like this: [wrapped for readability]

我的查询如下所示:[为可读性包装]

SqlDataSource1.SelectCommand = "SELECT [index] AS idex, store_number AS snum, 
    store_name AS sname, store_username AS suser, store_password AS spass, 
    store_count AS scount 
    FROM Stores 
    WHERE store_name = '" & Session("storename") & "'"

Very sloppy, but hopefully will work for what I need. The little I understand about variables should mean that the field of index should be stored to a variable named idex? is this correct? How do I use it later?

非常草率,但希望能满足我的需要。我对变量的了解应该意味着索引字段应该存储到名为idex的变量中?这样对吗?以后如何使用?

How do I take a variable from the column and put it in something like a text box,

我如何从列中取出一个变量并将其放入文本框之类的东西中,

回答by Szymon

The basic structure of the code is below.

代码的基本结构如下。

Open the connection. Best to do using Usingstructure. Create the command (again, Usingstructure). Execute the command and get the value.

打开连接。最好使用Using结构来做。创建命令(再次,Using结构)。执行命令并获取值。

Dim idx As Integer ' the variable to hold your index

Using conn As SqlConnection = New SqlConnection("your connection string") ' put your connection string here or get it from a config file
    conn.Open()
    Dim commandText As String = "SELECT [index] AS idex, store_number AS snum, store_name AS sname, store_username AS suser, store_password AS spass, store_count AS scount FROM Stores WHERE store_name = @storename"
    Using command As SqlCommand = New SqlCommand(commandText, conn)
        command.Parameters.Add(New SqlParameter("@storename", SqlDbType.VarChar, 50)).Value = "store name" ' replace the store name and the length of the field

        Using reader As SqlDataReader = command.ExecuteReader
            If reader.Read Then
                idx = reader.GetInt32(0) ' the first column
            End If
        End Using
    End Using
End Using


To get the connection string from your config file, do the following:

要从您的配置文件中获取连接字符串,请执行以下操作:

Add the reference to System.Configuration.dll

添加对 System.Configuration.dll 的引用

Add the connection string to your config file:

将连接字符串添加到您的配置文件中:

<connectionStrings>
    <add name="YourConnection" connectionString="Details"/>
</connectionStrings>

You can get your connection string from the code

您可以从代码中获取连接字符串

 Dim connStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("YourConnection").ConnectionString