Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
除非Mysql数据库在线,否则C#程序不会启动_C#_Mysql_Xml - Fatal编程技术网

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

除非Mysql数据库在线,否则C#程序不会启动,c#,mysql,xml,C#,Mysql,Xml,我的程序通常在Mysql离线时启动,它会打开,但会给出一个指示,表明该程序未连接到数据库,这正是我想要的。我希望即使数据库处于脱机状态,也能启动它,并给出一个未连接的指示器 我设置了一个连接指示器,如下所示: public void connStatus() { MySqlConnection myConn = new MySqlConnection("SERVER = localhost; user id = root; password =; data

我的程序通常在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();
                }
            }
        }
    Could not find schema information for the element 'setting'.
    Could not find schema information for the element 'value'.  
(我知道我上面的代码不是检查服务器连接的最佳代码,但我只需要一些东西来指示数据库是在线还是离线,我是编程新手,但我还不是很擅长。)

现在,在我添加了一些dll并继续我的小程序之后,只有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();
                }
            }
        }
    Could not find schema information for the element 'setting'.
    Could not find schema information for the element 'value'.  
所以我发现App.Config有问题, 问题是我对XML知之甚少,我不明白我的项目中的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>

我很高兴今天学到了一些新东西,多亏了google和stackoverflow,我今天才学会了正确的调试。 我试着用谷歌搜索与NullReference Exception相关的东西,我从这篇文章中找到了一个非常有用的指南,不是完全正确的答案,但帖子上的家伙用易于理解的术语为像我这样的新手解释了这一点。

尽管错误表明:

'System.NullReferenceException' occurred in MetroFramework.dll Additional information: Object reference not set to an instance of an object.
这让我作为一个初学者感到困惑,我在上面链接的帖子上读到,你需要检查InnerException, 我这样做了,导致了我的另一个错误

System.TypeInitializationException
这都是因为我从另一个类声明了一个变量

像我这样的新手可能会遇到同样的问题,所以我将分享帮助我的帖子。

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

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

现在它可以工作了:)多亏了上面那些有用的帖子。

我很高兴今天学到了一些新东西,多亏了google和stackoverflow,我今天才学会了正确的调试。 我试着用谷歌搜索与NullReference Exception相关的东西,我从这篇文章中找到了一个非常有用的指南,不是完全正确的答案,但帖子上的家伙用易于理解的术语为像我这样的新手解释了这一点。

尽管错误表明:

'System.NullReferenceException' occurred in MetroFramework.dll Additional information: Object reference not set to an instance of an object.
这让我作为一个初学者感到困惑,我在上面链接的帖子上读到,你需要检查InnerException, 我这样做了,导致了我的另一个错误

System.TypeInitializationException
这都是因为我从另一个类声明了一个变量

像我这样的新手可能会遇到同样的问题,所以我将分享帮助我的帖子。

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

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

现在它可以工作了:)多亏了上面的有用帖子。

你不能启动你的程序了吗?发生了什么错误?什么是错误消息?这是它所说的“MetroFramework.dll中发生了类型为'System.NullReferenceException'的未处理异常”。您可以尝试删除app.config中从
开始到关闭标记
结束的部分吗?当我删除您告诉我要删除的部分时,所有6条消息都说找不到元素“”的架构信息“,但程序仍然无法启动,并继续说,MetroFramework.dll中发生了类型为'System.NullReferenceException'的未处理异常。其他信息:对象引用未设置为对象的实例。我很困惑,甚至在这个问题发生之前,我就在使用MetroFramework了,它没有任何问题(错误发生在哪里?源代码行。您无法启动程序?出现一些错误?错误消息是什么?这是它所说的“MetroFramework.dll中发生了类型为'System.NullReferenceException'的未处理异常”。能否尝试删除app.config中从
开始到关闭标记
结束的部分?当我确实删除了您告诉我要删除的部分时,所有6条消息都显示“找不到元素“”的架构信息”,但该程序仍然无法启动,并继续说,MetroFramework.dll中发生了类型为“System.NullReferenceException”的未经处理的异常。其他信息:对象引用未设置为对象的实例。我很困惑,我甚至在该问题发生之前就在使用MetroFramework,并且没有任何问题(错误发生在哪里?源代码行。