从SQL中检索VB中的数据 [英] retrieving data in VB from SQL

查看:119
本文介绍了从SQL中检索VB中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Basic 2010和Microsoft SQL Server 2008.我有我的数据库和我的表和我做的连接(至少我想我做)在VB使用只有界面。

I use Visual Basic 2010 and Microsoft SQL Server 2008. I have my database and my table and i made the connection (at least i think i did) in VB using only the interface.

我想知道的是如何从数据库获取数据并将其用于我的VB项目。我当然搜索的解决方案已经,但差异我发现只有困惑我更多。我需要知道的是基础,工具/对象和检索数据的过程。

What i want to know is how to get data from the database and use it into my VB project. I have of course searched for solutions already but the differences i find only confuse me more. What i need to know are the basics, the tools/objects and procedures to retrieve the data.

我现在尝试做的是做一个简单的选择,将数据写入列表框中,就像这样:

What i try to do at the moment is make a simple selection and put that data into a listbox right when the program starts, like this:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SqlConnection1.Open()



        SqlConnection1.Close()

    End Sub
End Class


推荐答案

1)创建连接字符串

Dim connectionString As String = "Data Source=localhost;........."

2)连接到您的数据库

2) Connect to your Database

Dim connection As New SqlConnection(connectionString)
conn.Open()

3)创建命令和查询

Dim command As New SqlCommand("SELECT * FROM Product", connection)
Dim reader As SqlDataReader = command.ExecuteReader()  //Execute the Query

4)检索结果。有几种方法

4) Retrieve your result. There are several ways

Dim dt As New DataTable()
dt.Load(reader)

'Close the connection
connection.Close()

列表框

myListBox.ItemSource = dt

此处的完整代码

Using connection As New SqlConnection(connectionString)
    Dim command As New SqlCommand("Select * from Products", connection)
    command.Connection.Open()
    SqlDataReader reader = command.ExecuteReader()
 End Using

详情

  • SQLCommand

这篇关于从SQL中检索VB中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆