Vb.net 2010 连接到 SQL Server 2008

发布于 2024-11-30 03:46:29 字数 1185 浏览 3 评论 0原文

我正在尝试连接到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼忆七猫命九 2024-12-07 03:46:29

关于你的第一个问题, 如果我创建的连接字符串没问题

如果您没有遇到任何异常,则您已成功连接到数据库。

关于你的第二个问题。

你可以做两件事。

  1. 调试方式错误。 (不过确实有效)

    MsgBox(objdataSet.Tables[0].Rows.Count);

  2. 学习和调试正确的方法。

    阅读这些文章

    调试基础知识
    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.

  1. Debug the wrong way. (It works though)

    MsgBox(objdataSet.Tables[0].Rows.Count);

  2. 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.

农村范ル 2024-12-07 03:46:29

1) 如果我制作的连接字符串没问题

验证连接字符串的简单方法是使用 Visual Studio 中的“服务器资源管理器”进行连接。一旦获得良好的连接,右键单击该连接并获取“连接字符串”属性。

2)如何检查数据集是否产生任何结果?

MsgBox(objdataSet.GetXML)

GetXML 将以 XML 格式返回数据集中的所有数据。

另外,如果不需要,请不要全局声明变量。
声明它们更接近,或者根本不,就像这样......

Dim objdataSet As New Data.DataSet()

Using connectionString As New Data.SqlClient.SqlConnection("server=X86ONX64; database=students; Integrated Security=True")

    With New Data.SqlClient.SqlDataAdapter()
        .SelectCommand = connectionString.CreateCommand
        .SelectCommand.CommandText = "select * from basic_info;"
        .SelectCommand.CommandType = Data.CommandType.Text

        .Fill(objdataSet, "StudentInfo")
    End With

    MsgBox(objdataSet.GetXML)
End Using

'Now put it in your dataset...'

希望这有帮助。

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?

MsgBox(objdataSet.GetXML)

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...

Dim objdataSet As New Data.DataSet()

Using connectionString As New Data.SqlClient.SqlConnection("server=X86ONX64; database=students; Integrated Security=True")

    With New Data.SqlClient.SqlDataAdapter()
        .SelectCommand = connectionString.CreateCommand
        .SelectCommand.CommandText = "select * from basic_info;"
        .SelectCommand.CommandType = Data.CommandType.Text

        .Fill(objdataSet, "StudentInfo")
    End With

    MsgBox(objdataSet.GetXML)
End Using

'Now put it in your dataset...'

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文