如何在C#中检查我的实时数据库服务器是否已关闭 [英] How do I check if my live database server is down in C#

查看:227
本文介绍了如何在C#中检查我的实时数据库服务器是否已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个数据库服务器一个托管第二个托管在网络中,第三个是连接本地系统如果我的实时服务器或网络数据库已关闭我如何知道以及如何连接本地系统数据库。我正在使用实体框架



我尝试过:



公共类POSContext:DbContext

{

public POSContext():base(name = POSConnectionString)

{



}



#region系统

public DbSet< Company>公司{get;组; }



}



呼叫连接字符串



使用(var db = new POsContext())

{



}

I have 3 database server one is hosted Second one is hosted in network and third one is connected local system if my live server or network database is down how I know and how I connect local system database. I am working with entity framework

What I have tried:

public class POSContext : DbContext
{
public POSContext() : base("name=POSConnectionString")
{

}

#region System
public DbSet<Company> Company { get; set; }

}

calling connection string

using(var db=new POsContext())
{

}

推荐答案

在您的数据库中放置一个只返回1或其他内容的SP,然后使用类似GetPOSContext的方法,并在该方法中使用您的实时网站的连接字符串创建POSContext实例(您可以通过在POSContext的构造函数中传递连接字符串并尝试在该上下文上执行SP,如果有效则返回上下文。如果失败(即抛出异常),则使用本地数据库的连接字符串创建上下文,然后返回该上下文。当您的代码需要POSContext时,通过GetPOSContext方法获取它,并且您将始终获得一个有效的方法。
Put an SP in your database that simply returns "1" or something, then have a method like GetPOSContext and in that method create an instance of POSContext using the connection string for your live site (you can do this by passing the connection string in the constructor for POSContext) and try and execute the SP on that context and if it works return the context. If it fails (ie throws an exception) then create the context using the connection string for your local DB and return that context instead. When your code needs POSContext get it via the GetPOSContext method and you'll always get one that works.


这篇关于如何在C#中检查我的实时数据库服务器是否已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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