简体   繁体   English

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

[英]C# Program wont start unless Mysql database is Online

My program used to start even though Mysql is offline, it will open but give an indicator that the program is not connected to the database, which is what i want. 即使Mysql处于脱机状态,我的程序也曾经启动过,它会打开,但会指示该程序未连接到数据库,这就是我想要的。 I want it to start even if the database is offline and just give an indicator that its not connected I set a connection indicator like this: 即使数据库处于脱机状态,我也希望它启动,并给出一个指示其未连接的指示符,我将连接指示符设置如下:

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

(I know that my code above is not the best one for checking server connections, but i just needed something to indicate if the database is online or offline, im new to programming and im not very good at it yet.) (我知道我上面的代码不是检查服务器连接的最佳方法,但是我只需要一些信息来指示数据库是在线还是离线,这对编程来说是新手,还不是很擅长。)

now, after I added some dll's and went on with my little program, the program will only start if Mysql is Online. 现在,在我添加了一些dll并继续运行我的小程序之后,该程序仅在Mysql在线时才能启动。 It won't start at all and will give messages like this: 它根本不会启动,并且会给出如下消息:

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

so i figured out something is wrong at the App.Config, and the problem is I only have little knowledge on XML and i don't understand whats going on with the one on my project. 所以我发现App.Config出了点问题,问题是我对XML知之甚少,而且我不了解我项目中的那件事。 Can someone please tell me why is this happening? 有人可以告诉我为什么会这样吗? and how do i fix it? 以及我该如何解决? Any suggestions and corrections are highly appreciated. 任何建议和更正均受到高度赞赏。

this is the App.config of my project: 这是我的项目的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>

I am so happy i learned something new today,i just learned about proper debugging today thanks to google and stackoverflow. 我感到非常高兴,今天我学到了一些新东西,由于有了google和stackoverflow,我今天才了解了有关正确调试的知识。 I tried to google stuff related to NullReference Exception i found a very helpful guide from this post, not exactly the answer but the guy on the post explained it in easily understandable terms for newbies like me. 我试图用Google搜索与NullReference Exception相关的内容,我从这篇帖子中找到了一个非常有用的指南,虽然不完全是答案,但是帖子中的人用像我这样的新手容易理解的术语解释了它。 What is a NullReferenceException, and how do I fix it? 什么是NullReferenceException,如何解决?

though the error says: 尽管错误显示:

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

which got me confused as a beginner, i read on the post linked above that you need to check for the InnerException, which i did and led me to my other mistake which says 这让我对初学者感到困惑,我在上面的链接上阅读了您需要检查的InnerException信息,我这样做了,并导致我遇到另一个错误,该错误表示

System.TypeInitializationException

and it turns out its all because of a variable i declared from another class. 事实证明,这都是因为我从另一个类声明了一个变量。

someone whos a newbie like me might get the same problem so i'll share the posts that helped me. 像我这样的新手会遇到同样的问题,所以我将分享对我有帮助的帖子。 how to solve System.TypeInitializationException was unhandled exception in vb.net? 如何解决System.TypeInitializationException是vb.net中未处理的异常? Field xxx is never assigned to, and will always have its default value null 字段xxx永远不会分配给它,并且其默认值始终为null

and the final solution to my problem was i turned this: 而我的问题的最终解决方案是我把这个变成了:

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

into this: 到这个:

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

and now its working :) Thanks to the helpful posts above. 现在可以正常工作了:)感谢上面的有用帖子。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM