c# - 在这种情况下如何在 asp.net 中访问数据库?

标签 c# asp.net database

大家好,下面是我访问数据库的代码。当我尝试从多个选项卡打开站点或在 Debug模式下打开它时出现错误!

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DomeNameSpace
{
    public class DAL
    {
        public static string _ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["xClassConnectionString"].ConnectionString;
        public static SqlConnection _Connection = null;

        public static SqlConnection Connection
        {
            get
            {
                //_Connection.Close();
                //private static SqlConnection _Connection = null;
                if (_Connection == null)
                {
                    _Connection = new SqlConnection(_ConnectionString);
                    _Connection.Open();

                    return _Connection;
                }
                else if (_Connection.State != System.Data.ConnectionState.Open)
                {
                    _Connection.Open();

                    return _Connection;
                }
                else
                {
                    return _Connection;
                }
            }
        }

        public static DataSet GetDataSet(string sql)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                //   Connection.Close();
                DataSet ds = new DataSet();
                adp.Fill(ds);
                return ds;
            }
            catch (SqlException err)
            {
                // Replace the error with something less specific.
                // You could also log the error now.
                throw new ApplicationException("Data error. " + err.Message.ToString());
            }
            finally
            {
                Connection.Close();
            }
        }

        public static DataTable GetDataTable(string sql)
        {
            DataSet ds = GetDataSet(sql);

            if (ds.Tables.Count > 0)
                return ds.Tables[0];
            return null;
        }



        public static int ExecuteSQL(string sql)
        {
            try
            {
                string BegSql = "BEGIN TRY BEGIN TRANSACTION ";
                string EndSql = " COMMIT TRANSACTION END TRY BEGIN CATCH  ROLLBACK TRANSACTION END CATCH";
                string NewSql = BegSql + sql + EndSql;
                sql = NewSql;
                SqlCommand cmd = new SqlCommand(sql, Connection);
                return cmd.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                return -1;
            }
            finally
            {
                Connection.Close();
            }
        }

    }
}

但是我在这里遇到错误 enter image description here

输出结果为

enter image description here

问题是什么?

最佳答案

单个静态数据库连接是著名的坏主意。它本质上使您的应用程序成为单线程,而 Web 应用程序本质上不是。

不要像这样集中你的连接对象。创建连接对象不是资源密集型操作。打开连接本身并不是特别占用资源,连接池会为您处理大部分繁重的工作,并且经过了很好的优化。

在您需要时创建您的数据库连接对象,尽可能靠近您使用它们的地方,并在您使用完它们后立即处理它们。一般来说,一个类似这样的模式:

public void SomeMethodWhichConnectsToDB()
{
    using (var connection = new SqlConnection())
        using (var command = new SqlCommand())
        {
            // do something with the connection, execute the command, etc
        }
}

您可以将连接的创建封装到一个(非静态)方法中,以避免代码重复等。但是不要一遍又一遍地重复使用内存中的同一个连接对象。在尽可能短的时间内创建它、使用它、销毁它。

关于c# - 在这种情况下如何在 asp.net 中访问数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988940/

相关文章:

c# - 从列表集合中删除重复项

asp.net - 在.NET Core中更新身份登录页面的默认前端设计

php - 如何为简单的多语言网站(PHP MySQL)构建数据库?

MySQL如何对一名学生的科目成绩进行排名/位置

基于单元格文本的 Microsoft.Office.Interop.Excel.Cells 上的 C# 条件格式

c# - 如何将 IEnumerable<IEnumerable<T>> 转换为 IEnumerable<T>

c# - 如何将日期时间转换为两倍?

c# - 基本 LINQ to SQL GridView 绑定(bind)

c# - 使用 Twitterizer 发布推​​文

mysql - 安装脚本 - 如何检查数据库表是否已经存在?