除非 Mysql 数据库在线,否则 C# 程序不会启动

标签 c# mysql xml

我的程序以前即使 Mysql 处于离线状态也能启动,它会打开但会提示程序未连接到数据库,这正是我想要的。即使数据库处于脱机状态,我也希望它启动,只是给出一个未连接的指示器 我设置了这样的连接指示器:

public void connStatus()
        {
            MySqlConnection myConn = new MySqlConnection("SERVER = localhost; user id = root; password =; database = mpdb");
            try
            {
                myConn.Open();
                dbconsign.Visible = true;
                dbnotconsign.Visible = false;
            }
            catch (Exception ex)
            {
                MetroMessageBox.Show(this, ex.Message , "NOTIFICATION", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                dbconsign.Visible = false;
                dbnotconsign.Visible = true;
            }
            finally
            {
                if (myConn.State == ConnectionState.Open)
                {
                    myConn.Close();
                }
            }
        }

(我知道我上面的代码不是检查服务器连接的最佳代码,但我只需要一些东西来指示数据库是在线还是离线,我是编程新手而且我还不太擅长。)

现在,在我添加了一些 dll 并继续我的小程序之后,该程序将仅在 Mysql 在线时启动。它根本不会启动,并会给出如下消息:

    Could not find schema information for the element 'setting'.
    Could not find schema information for the element 'value'.  

所以我发现 App.Config 有问题, 问题是我对 XML 知之甚少,而且我不明白我的项目中发生了什么。 有人可以告诉我为什么会这样吗?我该如何解决? 非常感谢任何建议和更正。

这是我项目的 App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="cpsfmV2._0.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <userSettings>
        <cpsfmV2._0.Properties.Settings>
            <setting name="CurrentDate" serializeAs="String">
                <value />
            </setting>
        </cpsfmV2._0.Properties.Settings>
    </userSettings>
<system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.9.12.0" newVersion="6.9.12.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

最佳答案

我很高兴我今天学到了一些新东西,感谢谷歌和 stackoverflow,我今天刚刚学会了正确的调试。 我尝试在谷歌上搜索与 NullReference Exception 相关的内容,我从这篇文章中找到了一个非常有用的指南,虽然不完全是答案,但帖子中的人用易于理解的术语为像我这样的新手解释了它。 What is a NullReferenceException, and how do I fix it?

虽然错误说:

'System.NullReferenceException' occurred in MetroFramework.dll Additional information: Object reference not set to an instance of an object.

这让我作为初学者感到困惑,我在上面链接的帖子中读到你需要检查 InnerException, 我这样做并导致我犯了另一个错误

System.TypeInitializationException

这一切都是因为我从另一个类声明的变量。

像我这样的新手可能会遇到同样的问题,所以我会分享对我有帮助的帖子。 how to solve System.TypeInitializationException was unhandled exception in vb.net? Field xxx is never assigned to, and will always have its default value null

我的问题的最终解决方案是:

private AuditTrail audittrail = new AuditTrail();
private void loginbtn_Click(object sender, EventArgs e)
{
            //some code here
            audittrail.RecordLogin();
}

进入这个:

private AuditTrail audittrail;
private void loginbtn_Click(object sender, EventArgs e)
{
            audittrail = new AuditTrail();
            //some code here
            audittrail.RecordLogin();
}

现在开始工作了 :) 感谢上面的有用帖子。

关于除非 Mysql 数据库在线,否则 C# 程序不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55063295/

相关文章:

C#: 找不到 SerializableAttribute

xml - 如何保留标记标签?

c# - 如何使用数据库优先方法命名外键

c# - 通用谓词的默认值作为参数

c# - 将应用程序部署到 Windows Phone 时出错

mysql - 使用 LIMIT 对 MySQL 查询中的结果进行分页

mysql - 我可以在同一个 mysql 数据库中拥有希腊语、英语和中文吗?

java - 动态根元素 JAXB?

c# - 如何在 .Net Blazor 中获取 Javascript 回调?

mysql - 最适合这项工作的 DBMS 是什么?