如何转换

hahahahaha1234567890 2009-01-12 08:19:28
Public Shared Function GetStrConn(Optional ByVal vBolNeedCheckdata As Boolean = False) As String
On Error GoTo PROC_ERROR
Dim DBSet As New WinConfig
Dim StrConn As String
Dim Conn As New SqlClient.SqlConnection
Dim myCMD As SqlClient.SqlCommand
Dim StrSQL As String
Dim myReader As SqlClient.SqlDataReader

DBSet.XmlFileName = Microsoft.VisualBasic.Compatibility.VB6.GetPath & "\" & "WinConfig.xml"
DBSet.WindowName = "Data"
StrConn = "data source=" & DBSet.ReadData("ServerName") & ";initial catalog=" & DBSet.ReadData("DateBase") & ";password=" & DBSet.ReadData("PassWord") & ";persist security info=True;user id=" & DBSet.ReadData("User") & ";workstation id=PERMANI;packet size=4096;"
DBSet = Nothing

If vBolNeedCheckdata = True Then
'! 测试数据库
Conn.ConnectionString = StrConn
'If Conn.State = ConnectionState.Closed Then Conn.Open()
StrSQL = "select top 1 T_FileName from TM_FileVer"
myCMD = New SqlClient.SqlCommand(StrSQL, Conn)
If myCMD.Connection.State = ConnectionState.Closed Then
myCMD.Connection.Open()
End If
myReader = myCMD.ExecuteReader
myReader.Read()
If myCMD.Connection.State = ConnectionState.Open Then myCMD.Connection.Close()
End If
GetStrConn = StrConn
PROC_EXIT:
'* 此处可添加对变量的释放
If Not myReader Is Nothing Then
If myReader.IsClosed = False Then myReader.Close()
myReader = Nothing
End If
If Not myCMD Is Nothing Then
If myCMD.Connection.State = ConnectionState.Open Then myCMD.Connection.Close()
myCMD.Dispose()
End If
If Not Conn Is Nothing Then
Conn.Dispose()
End If
Exit Function
PROC_ERROR:
'* 此处为对错误的处理
GetStrConn = ""
MsgBox("数据库链接错误,请检查网络是否畅通,服务是否开启!", 48, "系统提示")
GoTo PROC_EXIT
End Function

请问这段VB代码,如何转换为C#
...全文
76 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hangang7403 2009-01-12
  • 打赏
  • 举报
回复
同意楼上的
wuyq11 2009-01-12
  • 打赏
  • 举报
回复
public static string GetStrConn( bool vBolNeedCheckdata)
{
string functionReturnValue = null;

WinConfig DBSet = new WinConfig();
string StrConn = null;
SqlClient.SqlConnection Conn = new SqlClient.SqlConnection();
SqlClient.SqlCommand myCMD = default(SqlClient.SqlCommand);
string StrSQL = null;
SqlClient.SqlDataReader myReader = default(SqlClient.SqlDataReader);

DBSet.XmlFileName = Microsoft.VisualBasic.Compatibility.VB6.GetPath + "\\" + "WinConfig.xml";
DBSet.WindowName = "Data";
StrConn = "data source=" + DBSet.ReadData("ServerName") + ";initial catalog=" + DBSet.ReadData("DateBase") + ";password=" + DBSet.ReadData("PassWord") + ";persist security info=True;user id=" + DBSet.ReadData("User") + ";workstation id=PERMANI;packet size=4096;";
DBSet = null;

if (vBolNeedCheckdata == true) {
Conn.ConnectionString = StrConn;
//If Conn.State = ConnectionState.Closed Then Conn.Open()
StrSQL = "select top 1 T_FileName from TM_FileVer";
myCMD = new SqlClient.SqlCommand(StrSQL, Conn);
if (myCMD.Connection.State == ConnectionState.Closed) {
myCMD.Connection.Open();
}
myReader = myCMD.ExecuteReader;
myReader.Read();
if (myCMD.Connection.State == ConnectionState.Open) myCMD.Connection.Close();
}
functionReturnValue = StrConn;
PROC_EXIT:
if ((myReader != null)) {
if (myReader.IsClosed == false) myReader.Close();
myReader = null;
}
if ((myCMD != null)) {
if (myCMD.Connection.State == ConnectionState.Open) myCMD.Connection.Close();
myCMD.Dispose();
}
if ((Conn != null)) {
Conn.Dispose();
}
return;
PROC_ERROR:

functionReturnValue = "";
Interaction.MsgBox("数据库链接错误,请检查网络是否畅通,服务是否开启!", 48, "系统提示");
goto PROC_EXIT;
return functionReturnValue;
}
再见品月 2009-01-12
  • 打赏
  • 举报
回复
..好长代码。。。飘过。。

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧