如何获取sql连接

发布于 2024-08-26 19:27:32 字数 1607 浏览 3 评论 0原文

FS_Setting 是一个 VB 类,其中包含连接的所有详细信息,即:

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

我需要在不同的类中调用函数 Get_RS_Connection(),而不是再次获取连接并进行硬编码......我想调用上面的内容声明 SQL 连接的类

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2024-09-02 19:27:32

以下代码更改将使代码可以从项目中的任何位置调用,而无需显式声明该类:

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

而不是:

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

您可以使用:

cn =FS_Setting.Get_RS_Connection

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文