处理C#中数据库连接切换的最佳方法 [英] Best way to handle Switching of database connection in C#

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

问题描述

Hello All,



我目前正在使用此方法连接SQL SERVER数据库



< pre lang =C#> String line = sr.ReadToEnd();

string connString = ValueCompair.Properties.Resources.ConnectionString.ToString();
// @Server = MDT825; DataBase = Test; User Id = TestUser; Password = TestUser @ 123 ;集成安全性= SSPI;

string query = line;

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query,conn);
conn.Open();





这里你看到我的连接字符串是动态的,用户可以连接到SQLSERVER或ORACLE。



我的问题是它也可以运行oracle吗?

如果不是建议我可以运行以下操作的常用方法对于SQLSERVER和ORACLE

1)连接数据库

2)运行查询

解决方案

我相信,我可能是错的,但是如果您针对DBConnection的基类进行编程,那么您可以为Oracle和SQL编写代码。



http://msdn.microsoft.com/en-us/library/system.data.common.dbconnection.aspx [ ^ ]


封装数据库总是一个好主意单例类中的se连接对象。我不认为''SqlConnection''类也适用于oracle。虽然我不太熟悉它在C#中是如何工作的,但我很确定它是微软特定的东西(因此特定于SQL Server)。你将需要一个用于oracle数据库连接的驱动程序和一个相应的类。


我认为你最好的方法可能是使用工厂模式



数据层抽象工厂 [ ^ ]


Hello All ,

I am currently using this method to connection SQL SERVER database

String line = sr.ReadToEnd();

                        string connString = ValueCompair.Properties.Resources.ConnectionString.ToString();
                            //@"Server=MDT825;DataBase=Test;User Id=TestUser;Password=TestUser@123;Integrated Security=SSPI";

                        string query = line;

                        SqlConnection conn = new SqlConnection(connString);
                        SqlCommand cmd = new SqlCommand(query, conn);
                        conn.Open();



Here as you see my connection string is dynamic and user can connection either to SQLSERVER OR ORACLE .

My question is Can it run for oracle as well ?
IF not the suggest me common method for below operation which can run for SQLSERVER and ORACLE
1) Connecting database
2) Running Query

解决方案

I believe, and I may be wrong, but if you program against the base class of DBConnection then you can code for both Oracle and SQL.

http://msdn.microsoft.com/en-us/library/system.data.common.dbconnection.aspx[^]


It''s always a good idea to encapsulate the database connection object within a singleton class. I don''t think that ''SqlConnection'' class will work with oracle as well. Even though I''m not quite familiar how it works in C#, I''m pretty sure it''s a microsoft specific thing (therefore SQL Server specific). You will need a driver for oracle database connection and a respective class for it.


I think your best approach might be to use the factory pattern

data layer abstract factory[^]


这篇关于处理C#中数据库连接切换的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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