简体   繁体   English

创建一个app.config文件

[英]Creating a app.config file

I have put together this code and was wondering how to call certain data from a .config or .ini file. 我把这段代码放在一起,想知道如何从.config或.ini文件中调用某些数据。 I have looked into AppSettings and ConfigurationManager but it's just too much for the noob brain to compute :-) 我已经研究了AppSettings和ConfigurationManager,但是对于菜鸟的大脑来说,这太过计算了:-

I have added the system.configuration reference class to the references class list. 我已经将system.configuration参考类添加到了参考类列表中。 Basically I want the variables for the MySQL stored on an external file that can be changed by an end user at a later time. 基本上,我希望存储在外部文件中的MySQL变量可以由最终用户在以后更改。 I Could not put the whole code because it said I have exceeded 3000 lines or something. 我无法放入整个代码,因为它说我已经超过了3000行或其他内容。

DBServer = RemoteServer

dataBase = myDatabase

user = root

password = [the password here]

I have tried the following code with app.config but still nothing 我已经尝试了以下代码与app.config但仍然没有

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="constring" connectionString="server=localhost; database=SchoolDB; user     id=root; password=Pa55w0rd;"/>
  </connectionStrings>
</configuration>

Replaced the following in the Mainwindow.cs 替换了Mainwindow.cs中的以下内容

string constring = "datasource=localhost;port=3306;username=root;password=Pa55w0rd";

with this 有了这个

string constring = ConfigurationManager.ConnectionStrings["dbconnectionstring"].ConnectionString;

Then I get this error 然后我得到这个错误

"Object reference not set to an instance of an Object" “你调用的对象是空的”

Ok so here is the Mainwindow.cs 好的,这是Mainwindow.cs

public partial class Portal : Form
{      
    public Portal(string UserName)
    {
        InitializeComponent();
        lbl_username.Text = UserName;
        timer1.Start();
        txt_staff_password1.PasswordChar = '*';
        load_loan_laptops();            
    }
    DataTable dbdataset;

    void load_loan_laptops()
    {
        //string constring = "datasource=localhost;port=3306;username=root;    password=Pa55w0rd";
        string constring =     ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        string Query = "Select * from schooldb.loans where Avail = 'yes';";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                string devices = myReader.GetString("Device_Name");
                cmb_acer_loan.Items.Add(devices);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        conDataBase.Close();
    }

Please can someone help with this issue. 请有人帮忙解决这个问题。

problem : Your app.config file has <connectionString> tag instead of <connectionStrings> tag. 问题:您的app.config文件具有<connectionString>标记而不是<connectionStrings>标记。

Solution: Replace <connectionString> with <connectionStrings> and </connectionString> with </connectionStrings> in app.config file 解决方案:app.config文件中,将<connectionString>替换为<connectionString><connectionStrings> </connectionString>替换为</connectionStrings>

Try This: 尝试这个:

app.config app.config

  <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="constring" connectionString="datasource=localhost;port=3306;username=root;password=Pa55w0rd"/>
      </connectionStrings>
</configuration>

code: 码:

string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;

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

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