当我的程序无法连接到数据库时如何显示错误消息?
当我编译程序并且用户或密码错误时,我需要知道一种显示消息或类似内容的方法,因为当我这样做时什么也没有发生。
我正在制作一个保存日志的系统。
log4net 仅使用 C#。如果你能帮助我,我将不胜感激。
谢谢!
I need to know a way to show a message or something like that when I compile my program and the user or password are wrong, because when I do this nothing happens.
I'm making a system that save logs.
log4net just using C#. if you can help me I'd appreciate.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,我认为没有办法在编译时做到这一点。
假设您的意思是运行时而不是编译时更有意义。当您尝试打开连接时,使用 try-catch 块,并在 catch 块内记录错误,显示警报,执行您需要执行的任何操作。
这是您所需要的还是您可以给出更具体的问题?
I don't think there is a way to do it at compile time, as far as I know.
Assuming you meant run time instead of compile time makes more sense. When you try to open the connection, use a try-catch block, and inside the catch block, log the error, display an alert, do whatever you need to do.
Is this what you need or can you give a more specific problem?
如果绝对要求在连接字符串无效时不构建项目,您可以执行类似于以下操作的操作。
塔达!除非连接字符串有效,否则项目将不会构建。
请记住,这仅验证来自构建机器的连接字符串是否有效。它不验证是否可以从其他任何地方访问它。
If it is absolutely a requirement that the project not build if the connection string is invalid you can do something that is approximately like the following.
TaDa! The project will not build unless the connection string is valid.
Keep in mind, this only validates that the connection string is valid from the build machine. It does not validate that it can be accessed from anywhere else.