我想要一个非常简单的VS 2010代码,用于从sql数据库中读取数据

我想要一个非常简单的VS 2010代码,用于从sql数据库中读取数据

问题描述:

我是VS 2010的新手
我正在使用asp.net
我想在Vs 2010 asp.net中创建一个Web表单,代码必须在VB中

我与本地数据库建立了连接,并且可以正常使用
我在页面上放了2个文本框和一个按钮

现在我想要当我按下那个按钮时
第一个记录显示在文本框中

我想要一个简单的例子
请帮我解决这个问题

谢谢
js

I am new in VS 2010
I am using asp.net
I want to create a web form in Vs 2010 asp.net, code must be in VB

I created a connection with my local database and it is working
I put 2 text box on page and a button

Now i want when i press that button then
the first one record shows in the text box

I want a simple example
Please help me to solve this problem

Thank you
js

CREATE TABLE Users(
	ID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
	NAME nvarchar(50) NULL,
 )
  
 INSERT INTO [Users] VALUES ('ABC')
 INSERT INTO [Users] VALUES ('DEF')
 INSERT INTO [Users] VALUES ('GHI')
 INSERT INTO [Users] VALUES ('JKL')







Protected Sub btnshow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnshow.Click
        Dim cn As SqlConnection
        vd = New SqlConnection("YOUR CONNECTION STRING")
        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If
        Dim da As SqlDataAdapter = New SqlDataAdapter()
        Dim ds As DataSet = New DataSet()
        Dim query As String = "SELECT * FROM [User]"
        'You can write your own query here as per your requirement
        da.SelectCommand = New SqlCommand(query, cn)
        da.SelectCommand.ExecuteScalar()
        ds.Clear()
        da.Fill(ds)
        txtID.Text = ds.Tables(0).Rows(0)(0).ToString()
        txtName.Text = ds.Tables(0).Rows(0)(1).ToString()
        cn.Close()


    End Sub