求用vb.net写一个读取数据库数据的简单操作。 20
假如我有个数据库test,表user,我想用vb.net连接本地数据库,数据库账户sa密码123,然后读取user表中所有数据(select*fromuser),将读出的...
假如我有个数据库test,表user,我想用vb.net连接本地数据库,数据库账户sa密码123,然后读取user表中所有数据(select * from user),将读出的数据一行一行输出到页面。代码越简单越。求高手代码
输出格式:<1>数据1</1><2>数据2</2>..
.
.
.
<1>数据1</1><2>数据2</2>.. 展开
输出格式:<1>数据1</1><2>数据2</2>..
.
.
.
<1>数据1</1><2>数据2</2>.. 展开
1个回答
展开全部
Option Explicit On
Option Strict On
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Program
Public Shared Sub Main()
Dim connectionString As String = _
"Data Source=(local);Initial Catalog=Northwind;" _
& "Integrated Security=true"
' Provide the query string with a parameter placeholder.
Dim queryString As String = _
"SELECT ProductID, UnitPrice, ProductName from dbo.Products " _
& "WHERE UnitPrice > @pricePoint " _
& "ORDER BY UnitPrice DESC;"
' Specify the parameter value.
Dim paramValue As Integer = 5
' Create and open the connection in a using block. This
' ensures that all resources will be closed and disposed
' when the code exits.
Using connection As New SqlConnection(connectionString)
' Create the Command and Parameter objects.
Dim command As New SqlCommand(queryString, connection)
command.Parameters.AddWithValue("@pricePoint", paramValue)
' Open the connection in a try/catch block.
' Create and execute the DataReader, writing the result
' set to the console window.
Try
connection.Open()
Dim dataReader As SqlDataReader = _
command.ExecuteReader()
Do While dataReader.Read()
Console.WriteLine( _
vbTab & "{0}" & vbTab & "{1}" & vbTab & "{2}", _
dataReader(0), dataReader(1), dataReader(2))
Loop
dataReader.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Using
End Sub
End Class
这是我在vs2010中微软自带的MSDN示例代码里面拷的,是关于ADO.net连接sql的操作。
希望对你有帮助。 如果你还需要其他的,我也可以再拷给你看。
Option Strict On
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Program
Public Shared Sub Main()
Dim connectionString As String = _
"Data Source=(local);Initial Catalog=Northwind;" _
& "Integrated Security=true"
' Provide the query string with a parameter placeholder.
Dim queryString As String = _
"SELECT ProductID, UnitPrice, ProductName from dbo.Products " _
& "WHERE UnitPrice > @pricePoint " _
& "ORDER BY UnitPrice DESC;"
' Specify the parameter value.
Dim paramValue As Integer = 5
' Create and open the connection in a using block. This
' ensures that all resources will be closed and disposed
' when the code exits.
Using connection As New SqlConnection(connectionString)
' Create the Command and Parameter objects.
Dim command As New SqlCommand(queryString, connection)
command.Parameters.AddWithValue("@pricePoint", paramValue)
' Open the connection in a try/catch block.
' Create and execute the DataReader, writing the result
' set to the console window.
Try
connection.Open()
Dim dataReader As SqlDataReader = _
command.ExecuteReader()
Do While dataReader.Read()
Console.WriteLine( _
vbTab & "{0}" & vbTab & "{1}" & vbTab & "{2}", _
dataReader(0), dataReader(1), dataReader(2))
Loop
dataReader.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Using
End Sub
End Class
这是我在vs2010中微软自带的MSDN示例代码里面拷的,是关于ADO.net连接sql的操作。
希望对你有帮助。 如果你还需要其他的,我也可以再拷给你看。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询