【发布时间】:2012-04-08 10:41:28
【问题描述】:
我正在尝试在 VB 中显示来自 SQL 服务器的查询结果。我写了以下代码,但没有得到如何“只显示结果”;
Public Function ConnectToSQL() As String
Dim con As New SqlConnection
Try
con.ConnectionString = "Data Source=(local);Initial Catalog=TestDatabase;Persist Security Info=True;User ID=sa;Password=afm"
Dim cmd As New SqlCommand("SELECT username FROM users WHERE username='user'", con)
con.Open()
Console.WriteLine("Connection Opened")
' Execute Query
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Console.WriteLine(String.Format("{0}, {1}", _
reader(0), reader(1)))
End While
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close() 'Whether there is error or not. Close the connection.
End Try
Return reader
End Function
任何人都可以指导吗?谢谢
【问题讨论】:
-
请阅读教程并发布less questions
-
@mola10 谢谢你的回答,你能在VB中给我一些指导吗。谢谢
-
google + "vb SqlDataReader" == 第一个位置 =) 链接:msdn.microsoft.com/en-us/library/haa3afyz(v=vs.71).aspx
-
这还能编译吗?您正在从声明为返回字符串的方法返回 sqldatareader 对象。
标签: sql-server vb.net visual-studio ado.net