如何访问DB在asp.net在这种情况下? [英] How to access DB in asp.net on this scenario?

查看:83
本文介绍了如何访问DB在asp.net在这种情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,大家好下面是我的code来访问数据库。当我尝试从多个选项卡中打开网站或我在调试模式下打开它提示错误!

Hello Guys Below is my Code for accessing database. when i try to open site from more than one tab or i open it in debugging mode it gives error!

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

    }
}

但我得到以下错误在这里

But i am getting following here error

和输出称

似乎是什么问题?

推荐答案

一个单一的,静态的数据库连接是的著名的的坏主意。这基本上使您的应用程序单线程的,它本质上是一个Web应用程序是没有的。

A single, static database connection is a famously bad idea. It essentially makes your application single-threaded, which a web application by nature is not.

不要集中这样的连接对象。创建一个连接对象不是一个资源密集型操作。而打开连接本身并不特别资源密集型的,连接池发生的最繁重你照顾和是很好的优化。

Don't centralize your connection object like this. Creating a connection object is not a resource-intensive operation. And opening the connection itself isn't particularly resource-intensive, the connection pool takes care of most of the heavy lifting for you and is very well optimized.

创建数据库连接对象,当你需要它们,因为接近的地方,你使用它们成为可能,并且你与他们做尽快自行处理。在一般情况下,一个图案与此类似:

Create your database connection objects when you need them, as close to where you use them as possible, and dispose of them as soon as you're done with them. In general, a pattern similar to this:

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

您可以封装的创建的连接到一个(非静态)方法,以避免code重复和诸如此类的东西。但是,不要在内存中一遍遍重复使用相同的连接对象。创建它,使用它,在短短的时间尽可能摧毁它。

You can encapsulate the creation of the connection into a (non-static) method to avoid code duplication and whatnot. But don't re-use the same connection object in memory over and over. Create it, use it, destroy it in as little time as possible.

这篇关于如何访问DB在asp.net在这种情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆