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

本文关键字:读取 Xml 文件 Load 执行 Form | 更新日期: 2023-09-27 18:03:49

我有一个c#应用程序,并使用xml文件设置连接字符串到我的sql数据库。数据库用tableadapters填充datagridviews。我希望在datagridview被填充之前设置连接字符串,因为我有一个datagridview的cellvaluechange事件。

现在我的代码代表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);

但我得到一个错误,连接字符串没有设置,在CellValueChanged.

我的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>

如果你能告诉我我错在哪里就太好了。

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

进入windows窗体项目的 program .cs文件。它看起来像:

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

相关部分是Main()的最后一行。表单在这里创建。在调用表单的构造函数之前,需要加载XML文件。可能的代码如下:

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

将加载的XmlNode(或任何您想要的)通过表单的构造函数传递给表单。因此,您必须在Form1.designer.cs文件中添加一个构造函数,并处理传递的数据。

我有一个几乎相同的程序。我像这样建立我的连接:

/// <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;
    }


在那之后,你必须告诉你的数据集使用这个连接,否则数据集中不会有数据,你会得到错误的connectionstring没有为你的数据集设置!它想要更改网格中的数据(因为可能您将其绑定到网格),但由于缺少连接而无法获取任何数据。

设置设计器制作的数据集的connectionstring可以这样做:

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

对我来说有效的解决方案是简单地创建一个函数,它读取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);
        }