数据库连接问题 [英] database connectivity problem

查看:71
本文介绍了数据库连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单独的类中创建数据连接dataadapter,数据集。在我们的主类中调用这个类??

解决方案

我建​​议阅读这些文章:

教程1:创建数据访问层 [ ^ ]

使用ADO.NET为您的应用程序实现数据访问层 [ ^ ]



您将找到许多有用的示例: CP知识库 [ ^ ]


你打算使用设计模式。如果是单身模式,这是最好的



 公开 密封  DBSingleton 
{
private static readonly DBSingleton dbInstance = new DBSingleton();

private readonly SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings [ db]。ConnectionString );

// 显式静态构造函数告诉C#编译器
< span class =code-comment> // 不将类型标记为beforefieldinit
static DBSingleton
{
}

private DBSingleton
{
}

public static DBSingleton实例
{
get { return dbInstance; }
}

public SqlConnection GetDBConnection()
{
返回 con;
}
}


How to make data connectivity dataadapter, dataset in seperate class. & call this class in our main class??

解决方案

I would suggest to read these articles:
Tutorial 1: Creating a Data Access Layer[^]
Implement a Data Access Layer for Your App with ADO.NET[^]

Many useful examples you'll find on: CP Knowledge Base[^]


are you going to use design patterns.if it is singleton patterns this is best

public sealed class DBSingleton
{
    private static readonly DBSingleton dbInstance = new DBSingleton();

    private readonly SqlConnection con =
        new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static DBSingleton
    {
    }

    private DBSingleton
    {
    }

    public static DBSingleton Instance
    {
        get { return dbInstance; }
    }

    public SqlConnection GetDBConnection()
    {
        return con;
    }
}


这篇关于数据库连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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