简体   繁体   English

在执行Form_Load之前如何读取Xml文件?

[英]How to read Xml file before execute Form_Load?

I have an C# application and use a xml file to set the connection string to my sql Database. 我有一个C#应用程序,并使用xml文件将连接字符串设置为sql数据库。 The database fills datagridviews with tableadapters. 数据库用表适配器填充datagridviews。 I want the connection string to be set before the datagridview is filled because i have a Event of CellValueChanged of the DataGridView. 我希望在填充datagridview之前设置连接字符串,因为我有一个DataGridView的CellValueChanged事件。

My code as it stands for the From_Load now is: 我现在代表From_Load的代码是:

xmldoc.Load("D:\\XML\\paths.xml");
            XmlNode node = xmldoc.DocumentElement.SelectSingleNode("/paths/sqlconnection");
            sqlconnect = node.InnerText;
            cn = new SqlCeConnection("Data Source=" + sqlconnect);


            // TODO: Diese Codezeile lädt Daten in die Tabelle "database1DataSet.Raum". Sie können sie bei Bedarf verschieben oder entfernen.
            this.raumTableAdapter1.Fill(this.database1DataSet.Raum);
            // TODO: Diese Codezeile lädt Daten in die Tabelle "database1DataSet.Firma". Sie können sie bei Bedarf verschieben oder entfernen.
            this.firmaTableAdapter1.Fill(this.database1DataSet.Firma);
            // TODO: Diese Codezeile lädt Daten in die Tabelle "database1DataSet.Kunde". Sie können sie bei Bedarf verschieben oder entfernen.
            this.kundeTableAdapter1.Fill(this.database1DataSet.Kunde);
            // TODO: Diese Codezeile lädt Daten in die Tabelle "database1DataSet.Ansprechperson". Sie können sie bei Bedarf verschieben oder entfernen.
            this.ansprechpersonTableAdapter1.Fill(this.database1DataSet.Ansprechperson);

But I get an error that the connection string is not set, at the time the CellValueChanged. 但是在CellValueChanged时出现一个未设置连接字符串的错误。

My XML File is set up like this: 我的XML文件的设置如下:

<?xml version="1.0" encoding="UTF-8"?>
- <paths>
    <sqlconnection>D:\\BDTWelcome - Kopie 2.0 fixed\\BDTWelcome\\Database1.sdf</sqlconnection>
    <ExcelVorlagen>D:\\BröExcelVorlagen</ExcelVorlagen>
  </paths>

It would be awesome if you could tell me where my mistake is. 如果您能告诉我我的错误在哪里,那就太好了。

Go to the Programm.cs file of your winfows forms project. 转到winfows窗体项目的Programm.cs文件。 It will look like: 它看起来像:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

The relevant part is the last line of the Main() . 相关部分是Main()的最后一行。 Here the form gets created. 在这里创建表格。 You need to load your XML file before the constructor of the form is called. 您需要在调用表单的构造函数之前加载XML文件。 A possible code could look like: 可能的代码如下所示:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    xmldoc.Load("D:\\XML\\paths.xml");
        XmlNode node = xmldoc.DocumentElement.SelectSingleNode("/paths/sqlconnection");
        sqlconnect = node.InnerText;
        cn = new SqlCeConnection("Data Source=" + sqlconnect);

    Application.Run(new Form1(XmlNode));
}

Pass the loaded XmlNode (or whatever you want) to the form throught it´s constructor. 通过其构造函数将加载的XmlNode(或任何您想要的)传递给表单。 Therefore you have to add a constructor in your Form1.designer.cs file and handle the passed data. 因此,您必须在Form1.designer.cs文件中添加一个构造函数并处理传递的数据。

I have a program doing nearly the same. 我有一个程序几乎在做。 I establish my connection like this: 我这样建立连接:

/// <summary>
    /// Datenbank wird geöffnet.
    /// </summary>
    /// <param name="sCon">Verbindungsstring.</param>
    /// <returns>Erfolgreich?</returns>
    public bool OpenDBConnect(string sCon)
    {
        bool bOK = false;

        try
        {
            _conn = new SqlConnection(sCon);
            _sConn = sCon;
            _conn.Open();
            bOK = true;
        }
        catch (Exception ex)
        {
            _Logger.Log(Properties.EnglishStringResource.ConnectionFailed + ex.Message);                
            bOK = false;
        }
        return bOK;
    }


After that you have to tell you dataset to use this connection of course, otherwise there will be no data in the dataset and you will get the error that the connectionstring is not set for your dataset! 之后,您必须告诉数据集当然要使用此连接,否则数据集中将没有数据,并且会出现错误,即未为数据集设置连接字符串! It want´s to change the data in your grid (because propably you bind it to the grid), but can´t get any data because of the missing connection. 它想更改网格中的数据(因为可能是您将其绑定到网格),但是由于缺少连接而无法获取任何数据。

Setting the connectionstring of a designer made dataset can be done like this: 可以通过以下方式设置设计师设计的数据集的连接字符串:

[ApplicationNamespace].Properties.Settings.Default["ConnectionString"] = newconstr;

The solution which worked for me is to simply create a function which reads the xlm file and sets the connection string right before the connection string is used for the first time. 对我有用的解决方案是简单地创建一个函数,该函数读取xlm文件并在首次使用连接字符串之前设置连接字符串。

public void readconnectionstring()
        {
            xmldoc.Load("D:\\XML\\paths.xml");
            XmlNode node = xmldoc.DocumentElement.SelectSingleNode("/paths/sqlconnection");
            sqlconnect = node.InnerText;
            cn = new SqlCeConnection("Data Source=" + sqlconnect);
        }

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

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