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

时间:2019-03-08 12:30:35

标签: 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();
                }
            }
        }

(我知道我上面的代码不是检查服务器连接的最佳方法,但是我只需要一些信息来表明数据库是在线还是离线,这对编程来说是新手,还不是很擅长。)< / p>

现在,在我添加了一些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>

1 个答案:

答案 0 :(得分:0)

我很高兴我今天学到了一些新知识,由于有了google和stackoverflow,我才刚刚了解了有关正确调试的知识。 我试图用Google搜索与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();
}

现在可以正常工作了:)感谢上面的有用帖子。