如何在VB.net中运行2个查询

时间:2014-02-09 23:17:44

标签: sql vb.net

我有一个简单的VB.net程序,我查询SQL数据库。这个程序工作正常,但我想在关闭数据库连接之前运行2个查询...如何在不混淆2个查询的信息的情况下执行此操作?

我的SQL部分代码如下所示:

Try
    For icounter = 1 To 2
        Call GetLocationInfo()
        connectionString = "Data Source=" & LocationDB & ";Initial Catalog=database;Persist Security Info=True;User ID=login;Password=password"
        sql = "select count(sTicket_number) as tickets from tickets where dtcreated between @StartDate AND @EndDate"
        sqlCnn = New SqlConnection(connectionString)

        sqlCnn.Open()
        sqlCmd = New SqlCommand(sql, sqlCnn)
        sqlCmd.Parameters.AddWithValue("@StartDate", dtstartdate)
        sqlCmd.Parameters.AddWithValue("@EndDate", dtenddate)

        Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
        While sqlReader.Read()
            msgbox(sqlReader.Item(0))
        End While
        sqlReader.Close()
        sqlCmd.Dispose()
        sqlCnn.Close()

1 个答案:

答案 0 :(得分:0)

由于你只运行2个查询,我会移动SqlCnn.Close() 在for循环之外。

并使用

if SqlCnn.state=closed then
   SqlCnn.open()
endif