为数据gridview选择不同的连接字符串 [英] Selecting different connection strings for data gridview
本文介绍了为数据gridview选择不同的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这不是直截了当的?我有两个数据库,每个数据库都包含一个日志表。我有一个存储过程,可以从表中提取数据。我在Windows窗体上有一个datagridview,并且有一个下拉框为各个数据库选择连接字符串。在选择conn字符串时,我想更改datagridview以在所选数据库中包含日志消息。
我的代码:
Why is this not straight-forward? I have two databases that contain a log-table each. I have a stored procedure that extracts the data from the table. I have a datagridview on a windows form, and a drop-down box to select the connection string for the respective databases. On selection of the conn string, I want to change the datagridview to contain the log messages in the selected database. My code:
Select Case cboConnection.Text
Case "CP DEV"
LogConnectionString = "Data Source=SAMBAR.gofast.com;Initial Catalog=CPDev;User ID=gofastconfig;Password=gofastdev;"
Case "CP LIVE"
LogConnectionString = "Data Source=SAMBAR.gofast.com;Initial Catalog=CPLive;User ID=gofastconfig;Password=gofastlive;"
End Select
Dim cmd As New SqlCommand("dbo.getLogMessages")
Using con As New SqlConnection(LogConnectionString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
sda.SelectCommand = cmd
sda.Fill(Me.CustomerPulseDBDataSet1)
con.Close()
gridLog.DataSource = Me.CustomerPulseDBDataSet1.Tables(0)
End Using
End Using
推荐答案
首先,我看不到您打开连接。然后,将它倒过来一点……
First, I don't see you open your connection. Then, you got it a bit up side down...
Using con As New SqlConnection(LogConnectionString)
con.Open()
Using cmd As New SqlCommand("dbo.getLogMessages", con)
cmd.CommandType = CommandType.StoredProcedure
Using da As New SqlDataAdapter(cmd)
' We need to clear out old data before reloading if same DS instance used
If Me.CustomerPulseDBDataSet1.Tables.Count > 0 Then
Me.CustomerPulseDBDataSet1.Tables.Clear()
End If
da.Fill(Me.CustomerPulseDBDataSet1)
End Using
End Using
con.Close()
End Using
gridLog.DataSource = Me.CustomerPulseDBDataSet1.Tables(0)
每次都应该完美工作
这篇关于为数据gridview选择不同的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文