任何人都可以请指导我如何将Visual Basic 2008连接到SQL Server 2008数据库?我很困惑什么是最好的做法,当涉及到这一点。如何将visual basic 2008连接到sql server 2008?
1
A
回答
1
有很多种方法。然而,这是使用.net库最简单的一种。
- 您连接到数据库使用的服务器,数据库和用户名和密码,
- 你发出一个SQL命令
- 你收集的结果在一个SqlDataReader
- 你迭代在读者的结果
- 你清理这些资源。
来源:How to ADO.NET SqlDataReader
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim sql As String
' Use this first connection string if using windows auth
' connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True"
connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
sql = "Your SQL Statement Here , like Select * from product"
sqlCnn = New SqlConnection(connectionString)
Try
sqlCnn.Open()
sqlCmd = New SqlCommand(sql, sqlCnn)
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
While sqlReader.Read()
MsgBox(sqlReader.Item(0) & " - " & sqlReader.Item(1) & " - " & sqlReader.Item(2))
End While
sqlReader.Close()
sqlCmd.Dispose()
sqlCnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class
0
相关问题
- 1. Visual Studio 2008不连接到SQL Server 2008
- 2. VB2010:连接到SQL Server 2008
- 3. asp.net连接到sql server 2008
- 4. 如何在Windows Server 2008上将SQL Server 2008与IIS 7连接
- 5. JDBC连接到SQL Server 2008
- 6. 如何连接到SQL Server 2008
- 7. 如何CakePHP的连接到SQL Server 2008
- 8. 将SQL Server 2008连接到Outlook日历?
- 9. 将Reporting Services 2005SP2连接到SQL Server 2008
- 10. 将python 3.3连接到microsoft sql server 2008
- 11. 将sql server 2008 r2连接到netbeans
- 12. 将Web API连接到SQL Server 2008
- 13. 将JIRA连接到SQL Server 2008
- 14. visual basic .NET连接到sql server compact 3.5
- 15. Visual Studio 2008与Sql Server 2005的连接
- 16. C++ ODBC SQL Server 2008连接
- 17. 如何从VS 2008连接到SQL Server 2008
- 18. SQL Server 2008加密连接
- 19. SqlDatasource连接到SQL Server 2008中的ASP.NET
- 20. 将Visual Studio 2010中的ASP.NET网站连接到SQL Server 2008
- 21. 在SQL Server连接表2008
- 22. SQL Server 2008连接问题
- 23. 从C#连接到SQL Server 2008#
- 24. JDBC连接SQL Server 2008中
- 25. 远程连接到SQL Server 2008 sp2
- 26. 无法连接到SQL Server 2008 R2
- 27. SQL Server 2008 R2 VB.net连接
- 28. Visual Basic 2008
- 29. 如何将SQL Server 2008 R2数据库还原到SQL Server 2008?
- 30. 如何连接的Microsoft SQL Server 2008 R2
你需要做一个教程在VB.net和SQL服务器 - 有数以百万计在网上(如例子中,我发现了以下)。 – 2012-01-02 11:15:44