2014-02-06 42 views
0

我有一个Winform,它只包含一个textbox对象,我试图用它作为T-SQL存储过程结果的存储库。这里是我用来连接数据库并运行存储过程的代码。将存储过程的结果配置为.NET文本框

SP似乎运行正常,但它不会以我期待的方式将信息返回到textbox。它根本不更新text属性。

Public Function ConnectToSQL() As String 
    Dim con As New SqlConnection 
    Dim reader As SqlDataReader 
    Try 
     con.ConnectionString = ("Data Source=" & Utilnamespace.SQLSvr & ";Database=Master" & ";integrated security=SSPI;") 
     Dim cmd As New SqlCommand("sp_whoisactive", con) 
     con.Open() 
     reader = cmd.ExecuteReader() 
     While reader.Read() 
      txtSQL.Text = String.Format("{0}", _ 
       reader(0)) 
     End While 
    Catch ex As Exception 
     MessageBox.Show("Error while connecting to SQL Server. " & ex.Message) 
    Finally 
     con.Close() 
    End Try 
    Return "Done" 
End Function 

我在这里做错了什么?

回答

2

你不应该串连吗?

While reader.Read() 
     txtSQL.Text += String.Format("{0}", _ 
      reader(0)) 
    End While 
相关问题