【问题标题】:The connection string property has not been initialized C# and XML连接字符串属性尚未初始化 C# 和 XML
【发布时间】:2017-10-10 05:23:57
【问题描述】:

我正在尝试测试连接并将凭据保存在配置文件中,但我不断收到此错误“尚未初始化 ConnectionString 属性”

这是 C# 代码

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            StringBuilder str = new StringBuilder("server=");
            str.Append(textBox1.Text);
            str.Append(";database=");
            str.Append(textBox2.Text);
            str.Append(";UID=");
            str.Append(textBox3.Text);
            str.Append(";password=");
            str.Append(textBox4.Text);
            string strcon = str.ToString();
            updateConfigFile(strcon);

            SqlConnection con = new SqlConnection();

            //Refresh Connection String each time else
            //it will use previous connection string.
            ConfigurationManager.RefreshSection("connectionStrings");
            con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;

            //Check the new connection string is working or not
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from egtab3");
            cmd.ExecuteNonQuery();
            MessageBox.Show("Connected Sucessfully");
            con.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    public void updateConfigFile(string con)
    {
        //Updating Config File
        XmlDocument xmldoc = new XmlDocument();

        //Loading the Config File
        xmldoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        foreach (XmlElement xElement in xmldoc.DocumentElement)
        {
            if (xElement.Name == "connectionStrings")
            {
                //Setting the connection string
                xElement.FirstChild.Attributes[4].Value = con;
            }
        }
        //writing the connection string in config file
        xmldoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    }

这是xml代码

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="con" providerName="using System.Data.SqlClient" 
connectionString=""/>
</connectionStrings>
</configuration>

【问题讨论】:

  • 在您的 app.config 中,connectionstring 值为空,在您的代码中,手动构建的 connectionstring 永远不会用于调用 updateConfigFile 方法(顺便说一下,我不知道 UID 是否是有效键对于 Sql Server)那么当您尝试打开连接时,您还期望发生什么?
  • 谢谢。我更新了代码。现在我收到此错误“传递的索引超出范围”。我现在该怎么办?
  • 计算连接字符串条目中预期的属性。共有三个属性,因此索引的正确范围是 0 到 2,其中 2 你应该使用而不是 4
  • 谢谢!那行得通。它说连接成功。现在问题是细节没有保存在配置文件中。我现在该怎么办?
  • 谢谢!做到了。当我独立运行时,内容保存在文件中。感谢您的宝贵时间。

标签: c# xml c#-4.0


【解决方案1】:

在从配置获取之前添加更新

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                StringBuilder str = new StringBuilder("server=");
                str.Append(textBox1.Text);
                str.Append(";database=");
                str.Append(textBox2.Text);
                str.Append(";UID=");
                str.Append(textBox3.Text);
                str.Append(";password=");
                str.Append(textBox4.Text);

                updateConfigFile(str);

                SqlConnection con = new SqlConnection();

                //Refresh Connection String each time else
                //it will use previous connection string.
                ConfigurationManager.RefreshSection("connectionStrings");
                con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;

                //Check the new connection string is working or not
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from egtab3");
                cmd.ExecuteNonQuery();
                MessageBox.Show("Connected Sucessfully");
                con.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

【讨论】:

  • 谢谢。我更新了代码。现在我收到此错误“传递的索引超出范围”。我现在该怎么办?
  • 我认为问题出在 xElement.FirstChild.Attributes[4].Value = con;你为什么设置为4?将索引更改为 2
  • 谢谢!那行得通。它说连接成功。现在问题是细节没有保存在配置文件中。我现在该怎么办?
猜你喜欢
  • 1970-01-01
  • 2017-09-14
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
相关资源
最近更新 更多