为什么这个mySQL语句会导致错误'已经有一个开放的DataReader

时间:2016-10-26 12:13:36

标签: mysql asp.net vb.net webforms

Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.Web
Partial Class admin_messages
   Inherits System.Web.UI.Page
   Dim ConnString As String = ConfigurationManager.ConnectionStrings("ConnString").ConnectionString
   Dim conn As New MySqlConnection(ConnString)
   Dim command As New MySqlCommand
   Dim ds As New DataSet
   Dim dr As MySqlDataReader
   Dim sql As String
   Dim sqlonDetail As String
   Dim sqloffDetail As String
   Dim sqlOn As String
   Dim sqlOff As String
   Dim da As MySqlDataAdapter
   Dim dt As New DataTable
   Dim mySQLStartDate As String
   Dim mySQLEndDate As String
   Dim filter As String
   Dim mySelectedRow As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
    loadMessages()
    End If
End Sub
Private Sub loadMessages()
    Me.MessageGridView.DataSource = Nothing
    Me.MessageGridView.DataBind()
    command.Connection = conn
    If conn.State <> ConnectionState.Open Then
        conn.Open()
    End If
    sql = "SELECT id,message_subject, CASE WHEN length(message) <= 39 THEN message ELSE CONCAT(SUBSTRING_INDEX(LEFT(message,40), ' ' , 12),'.....') end as message,target,status,DATE_FORMAT(sent_datetime, '%d/%m/%Y %H:%m:%s') as date_sent FROM msg_master ORDER BY sent_datetime DESC"
    da = New MySqlDataAdapter(sql, conn)
    da.Fill(ds, "messages")
    Me.MessageGridView.DataSource = ds.Tables(0)
    Me.MessageGridView.DataBind()
    Me.MessageGridView.Visible = True
    Me.MessageGridView.SelectedIndex = -1
    conn.Close()
End Sub

上面的代码导致错误'已经有一个与此Connection关联的打开的DataReader必须先在da.Fill(ds,"messages")处关闭,我不知道为什么。

如果我将SQL简化为:

sql = "SELECT id,message_subject,message,target,status, sent_datetime as date_sent FROM etiamfrs.frs_workforce_msg_master ORDER BY date_sent DESC" 

一切都很好。

如果我将原始sql复制到mySQLWorkbench并运行它,它可以正常工作。

My SQL的结构是将一个长文本字段修剪为一定数量的单词,然后将“...”附加到结尾,而不是将Gridview的大小调整为字段内容。

我现在已经将我的代码修改为以下内容但仍然得到同样的错误:

Me.MessageGridView.DataSource = Nothing
Me.MessageGridView.DataBind()
'command.Connection = conn
sql = "SELECT id,message_subject, CASE WHEN length(message) <= 39 THEN message ELSE CONCAT(SUBSTRING_INDEX(LEFT(message,40), ' ' , 12),'.....') end as message,target,status,DATE_FORMAT(sent_datetime, '%d/%m/%Y %H:%m:%s') as date_sent FROM etiamfrs.frs_workforce_msg_master ORDER BY date_sent DESC"
'sql = "SELECT id,message_subject,message,target,status, sent_datetime as date_sent FROM msg_master ORDER BY date_sent DESC"

Dim ConnString As String = ConfigurationManager.ConnectionStrings("ConnString").ConnectionString
Dim conn As New MySqlConnection(ConnString)
If conn.State = ConnectionState.Open Then
    conn.Close()
End If
Dim ds As New DataSet
Dim da As MySqlDataAdapter = New MySqlDataAdapter(sql, conn)
da.Fill(ds, "messages")
Me.MessageGridView.DataSource = ds.Tables(0)
Me.MessageGridView.DataBind()
Me.MessageGridView.Visible = True
Me.MessageGridView.SelectedIndex = -1

好的,我已设法在此时查询问题DATE_FORMAT(sent_datetime, '%d/%m/%Y %H:%m:%s') as date_sent

如果删除DATE_FORMAT位并将sent_datetime简化为date_sent,则查询有效。

0 个答案:

没有答案