简体   繁体   English

如何判断 SQL 服务器已启动并可以使用?

[英]How can I tell that SQL Server is started and ready to use?

I have an app that runs on system startup that needs to talk to the local SQL Server 2005 installation.我有一个在系统启动时运行的应用程序,它需要与本地 SQL Server 2005 安装进行通信。 I'm using the ServiceController class to wait for it to enter the "Running" state, and most times this works fine.我正在使用 ServiceController class 等待它进入“正在运行”state,而且大多数情况下都可以正常工作。 However, sometimes on a cold boot, my first query fails, saying that it couldn't log on the current user.但是,有时在冷启动时,我的第一个查询失败,说它无法登录当前用户。

It sounds like a race condition, but I figured my WaitForStatus call was supposed to resolve it.这听起来像是一个竞争条件,但我认为我的 WaitForStatus 调用应该可以解决它。

Is there a better way to make sure it's really-truly running?有没有更好的方法来确保它真正运行? Should I just throw in an extra sleep and assume it's long enough?我应该多睡一觉并假设它足够长吗?

You could just catch the exception and retry until it works...您可以捕获异常并重试,直到它起作用...

You could always use the System.Data.Sql.SqlDataSourceEnumerator and check for the local instance.您始终可以使用System.Data.Sql.SqlDataSourceEnumerator并检查本地实例。

On the plus, you can also discover version information with the enumerator.此外,您还可以使用枚举器发现版本信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据库未启动但 SQL Server 已准备好连接时的 SqlException - SqlException when the database is not started but SQL Server is ready for connections 如何告诉Entity Framework首先在代码中使用SQL Server Compact? - How do I tell Entity Framework to use SQL Server Compact in code first? 如何将TableDirect用于SQL Server CE? - How can I use TableDirect for SQL Server CE? 如何判断代码是否将在客户端或服务器上运行 Blazor? - How can I tell if code will run on the client or server with Blazor? 在自动启动服务中连接到SQL Server失败,在手动启动时有效。 SQL Server尚未就绪,但服务已启动 - Connecting to SQL Server fails in autostart service, works when starting manually. SQL Server not ready, yet service is started 我可以在属性上使用属性来告诉DataGridView如何格式化列吗? - Is there an Attribute I can use on a property to tell DataGridView how to format the column? 如何告诉AutoMapper在目标类型上使用方法? - How can I tell AutoMapper to use a method on my destination type? 我可以在具有相同实例的 SQL Server 上使用链接服务器到 SQL Server 吗? - Can I use linkserver to SQL Server on SQL Server with the same instance? SQL Server 2008:我可以为此使用约束吗? - SQL Server 2008: Can I use a constraint for this? 如何将数据从SQL Server数据集中提取到可以使用的类中? - How can I extract data from my SQL Server Dataset into a class that I can use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM