asp.net - 在静态类中存储连接 (ASP.NET)

标签 asp.net database class static

因为我使用的是 Postgresql 而不能使用 LINQ to SQL,所以我编写了自己的包装器类。

这是 Student 类的一部分:

public class Student : User
{
    private static NpgsqlConnection connection = null;

    private const string TABLE_NAME = "students";

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }

    /// <summary>
    /// Reads data from the data reader and moves it to the Student class.
    /// </summary>
    private static void ReadFields(Student student, NpgsqlDataReader dr)
    {
        student.Id = Int32.Parse(dr["id"].ToString());
        student.FirstName = dr["first_name"].ToString();
        student.LastName = dr["last_name"].ToString();
        student.Password = dr["password"].ToString();
    }

    /// <summary>
    /// Updates the student
    /// </summary>
    public void Update()
    {
        Connect();
        Run(String.Format("UPDATE " + TABLE_NAME + " SET first_name='{0}', last_name='{1}', password='{2}' WHERE id={3}", FirstName, LastName, Password, Id));
        connection.Dispose();
    }

    /// <summary>
    /// Inserts a new student
    /// </summary>
    public void Insert()
    {
        Connect();
        Run(String.Format("INSERT INTO " + TABLE_NAME + " (first_name, last_name, password) VALUES ('{0}', '{1}', '{2}')",FirstName, LastName, Password));
        connection.Dispose();
    }

    private static void Run(string queryString)
    {
        NpgsqlCommand cmd = new NpgsqlCommand(queryString, connection);
        cmd.ExecuteScalar();
        cmd.Dispose();
    }

    private static void Connect()
    {
        connection = new NpgsqlConnection(String.Format("Server=localhost;Database=db;Uid=uid;Password=pass;pooling=false"));
        connection.Open();
    }

    //....

因此,正如您在每个 INSERT、DELETE、UPDATE 请求中看到的那样,我正在使用连接到数据库的 Connect() 方法。在我不得不等待 10 分钟才能插入 500 行之前,我没有意识到这是多么愚蠢,因为有 500 个连接到数据库。

所以我决定将 Connection 属性移动到静态数据库类。

public static class DB
{
    private static NpgsqlConnection connection = null;
    public static NpgsqlConnection Connection
    {
        get
        {
            if (connection == null)
            {
                connection = new NpgsqlConnection(String.Format("Server=localhost;Database=db;Uid=uid;Password=pass;pooling=false"));
                connection.Open();
            }
            return connection;
        }
    }

    public static void Run(string queryString)
    {
        NpgsqlCommand cmd = new NpgsqlCommand(queryString, connection);
        cmd.ExecuteScalar();
        cmd.Dispose();
    }
}

现在可以了!我将 Student 类中的所有 Run 方法替换为 DB.Run

但我想知道它是否适用于很多人在线,而不仅仅是我。我不确定静态事物如何与 ASP.NET 一起工作,也许它会占用大量内存?..

最佳答案

最好不要将连接存储在静态字段中。按需创建连接对象并让连接池管理您的连接。

关于asp.net - 在静态类中存储连接 (ASP.NET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2383551/

相关文章:

c# - IHttpHandler 不会接受 DELETE 或 PUT 请求

C++ 错误 - '.' 标记前的预期主表达式|

python - 将不同的变量分配给不同的类

php - 在 PHP 中创建两个相同的静态类

asp.net - VB.NET创建两列CheckboxList

asp.net - 在 linux 上构建 web api .net 核心

asp.net - Web 配置转换不起作用

sql - 错误 : "No value given for one or more required parameters" when updating Database

SQL 2008 Express (R2) : Creating an UPDATE trigger to update a table on another server?

mysql - 数据库中的一对一关系