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

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

问题描述

Hello Guys下面是我的访问数据库的代码。当我尝试从多个选项卡打开网站或我在调试模式下打开它会给错误!

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

命名空间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)
{
//将错误替换为不太具体的错误。
//你现在也可以记录错误。
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交易;
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();
}
}

}
}

但我得到这里的错误



并输出



解决方案



单一的静态数据库连接是一个非常有名的主意。它本质上使你的应用程序单线程,一个web应用程序本质上是不。



不要这样集中你的连接对象。创建连接对象不是资源密集型操作。打开连接本身并不是特别耗费资源,连接池会为您处理大部分繁重工作,并且已进行了优化。



创建数据库连接对象,当你需要它们,尽可能接近你使用它们,并在处理完它们后尽快处理它们。一般来说,类似这样的模式:

  public void SomeMethodWhichConnectsToDB()
{
using连接=新的SqlConnection())
使用(var command = new SqlCommand())
{
//使用连接执行某些操作,执行命令等
}
}

您可以将连接的创建非静态)方法,以避免代码重复和whatnot。但是不要在内存中重复使用相同的连接对象。创建它,使用它,在尽可能少的时间内销毁它。


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

and output says

what seems to be the problem?

解决方案

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
        }
}

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.

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

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