开发者

how to get the sql connection

FS_Setting is a VB class which has all the details of the connections ie:

Public Class FS_Setting
    Public Function Get_RS_Connection() As SqlConnection
        Try
            Get_RS_Connection = New SqlConnection("Data Source=***********;User ID=sa;Password=*****;database=*********")
                             Catch ex As System.Exception
            Throw New System.Exception("Get_RS_Connection Error:" + ex.开发者_高级运维Message)
        End Try
    End Function

I need to call the function Get_RS_Connection() in a different class instead of getting the connection all the way again and hard coding.... I want to call the above class where the SQL connection is declared

Namespace FS_Library
       Public Class FS_Errorlog
        Inherits FS_BaseClass
Try

    **cn = New SqlConnection("Data Source=***********;UserID=sa;Password=*****;database=*********")**
    cmd = New SqlCommand("dbo.FS_ErrorLog_ADD", cn)
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandTimeout = Convert.ToInt32(ConfigurationSettings.AppSettings("Command_Timeout"))
    Me.AddParameter(cmd, "@p_tableKey", SqlDbType.Int, tableKey)
    Me.AddParameter(cmd, "@p_FunctionCode", SqlDbType.Int, FunctionCode)
    Me.AddParameter(cmd, "@p_TableAlias", SqlDbType.VarChar, TableAlias)
    Me.AddParameter(cmd, "@p_ValidationCode", SqlDbType.Int, ValidationCode)
    If Filename = "" Then
        Filename = "N/A"
    End If
    Me.AddParameter(cmd, "@p_FileName", SqlDbType.VarChar, Filename)
    Me.AddParameter(cmd, "@p_Message", SqlDbType.VarChar, Message)
    Me.AddParameter(cmd, "@p_CreateUser", SqlDbType.VarChar, userID)
    Me.AddParameter(cmd, "@p_UserActionID", SqlDbType.UniqueIdentifier, UserActionID)
    cn.Open()


The following code change will make the code callable from anywhere in your project without requiring an explicit declaration of the class:

Public Class FS_Setting
    Public Shared Function Get_RS_Connection() As SqlConnection
        Try
            Get_RS_Connection = New SqlConnection("Data Source=***********;User ID=sa;Password=*****;database=*********")
         Catch ex As System.Exception
            Throw New System.Exception("Get_RS_Connection Error:" + ex.Message)
        End Try
    End Function

Instead of:

cn = New SqlConnection("Data Source=***********;UserID=sa;Password=*****;database=*********")**

You can use :

cn =FS_Setting.Get_RS_Connection
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜