Vb.net 2010 连接到 SQL Server 2008
我正在尝试连接到 MS SQL SERVER 2008 数据库(学生),我想确保:
1)我制作的连接字符串是否正常
Dim connectionString As New SqlConnection("server=X86ONX64; database=students; Integrated Security=True")
2)如何检查数据集是否产生任何结果?
代码:
Public Class Form1
'Connection String + DataAdaptor + DataSet
'Declared Outside Any Subroutines/Functions
Dim connectionString As New SqlConnection("server=X86ONX64; database=students; Integrated Security=True")
Dim dataAdaptor As New SqlDataAdapter()
Dim objdataSet As New DataSet()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dataAdaptor.SelectCommand = New SqlCommand()
dataAdaptor.SelectCommand.Connection = connectionString
dataAdaptor.SelectCommand.CommandText = "select * from basic_info;"
dataAdaptor.SelectCommand.CommandType = CommandType.Text
connectionString.Open()
dataAdaptor.Fill(objdataSet, "StudentInfo")
connectionString.Close()
grd.AutoGenerateColumns = True
grd.DataSource = objdataSet
grd.DataMember = "basic_info"
dataAdaptor = Nothing
connectionString = Nothing
End Sub
I am trying to connection to MS SQL SERVER 2008 database (students), i want to make sure:
1) If connection string that i made is OK
Dim connectionString As New SqlConnection("server=X86ONX64; database=students; Integrated Security=True")
2) How can i check if dataset is producing any results?
Code:
Public Class Form1
'Connection String + DataAdaptor + DataSet
'Declared Outside Any Subroutines/Functions
Dim connectionString As New SqlConnection("server=X86ONX64; database=students; Integrated Security=True")
Dim dataAdaptor As New SqlDataAdapter()
Dim objdataSet As New DataSet()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dataAdaptor.SelectCommand = New SqlCommand()
dataAdaptor.SelectCommand.Connection = connectionString
dataAdaptor.SelectCommand.CommandText = "select * from basic_info;"
dataAdaptor.SelectCommand.CommandType = CommandType.Text
connectionString.Open()
dataAdaptor.Fill(objdataSet, "StudentInfo")
connectionString.Close()
grd.AutoGenerateColumns = True
grd.DataSource = objdataSet
grd.DataMember = "basic_info"
dataAdaptor = Nothing
connectionString = Nothing
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于你的第一个问题,
如果我创建的连接字符串没问题
如果您没有遇到任何异常,则您已成功连接到数据库。
关于你的第二个问题。
你可以做两件事。
调试方式错误。 (不过确实有效)
MsgBox(objdataSet.Tables[0].Rows.Count);
学习和调试正确的方法。
阅读这些文章
调试基础知识
Visual Studio 调试
调试基础知识:断点
无论如何,学习一下如何正确使用调试器、设置断点、在监视窗口中分析 objdataSet 对象并查看是否存在任何结果。
About your first question,
If connection string that i made is OK
If you did not get any exceptions, you've successfully made the connection to your database.
About your second question.
There are two things you can do.
Debug the wrong way. (It works though)
MsgBox(objdataSet.Tables[0].Rows.Count);
Learn and debug the correct way.
Read these articles
Debugging Basics
Visual Studio Debugging
Debugging Basics: Breakpoints
Anyway, learn how to use the debugger properly, put a break point, analyse the objdataSet object in a watch window and see if any results are present.
1) 如果我制作的连接字符串没问题
验证连接字符串的简单方法是使用 Visual Studio 中的“服务器资源管理器”进行连接。一旦获得良好的连接,右键单击该连接并获取“连接字符串”属性。
2)如何检查数据集是否产生任何结果?
GetXML 将以 XML 格式返回数据集中的所有数据。
另外,如果不需要,请不要全局声明变量。
声明它们更接近,或者根本不,就像这样......
希望这有帮助。
1) If connection string that i made is OK
Easy way to verify connection strings is to connect using the "Server Explorer" in Visual Studio. Once you get a good connection, right-click on the connection and just grab the "Connection String" property.
2) How can i check if dataset is producing any results?
GetXML will return ALL the data in the dataset in XML format.
Also, don't globally declare variables if you don't need to.
Declare them closer, or not at all, like so...
Hope this helps.