我做了app.config。现在我想创建新文件“.cs”,我想访问我在“app.config”中使用的凭据..我在现有的“.cs”中使用...但是想制作新的“.CS”文件......所以我很容易处理我想要做的改变..plz help
namespace CommoditiesControl.RatePuller
{
public class DBConnect
{
private MySqlConnection connection;
private string server;
private int port;
private string database;
private string uid;
private string password;
//Constructor
public DBConnect()
{
Initialize();
}
//Initialize values
private void Initialize()
{
string DBserver = ConfigurationManager.AppSettings["server"];
int DBport = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
string DBdatabase = ConfigurationManager.AppSettings["eagri"];
string uid = ConfigurationManager.AppSettings["test"];
string password = ConfigurationManager.AppSettings[""];
}
string connectionString;
connectionString = "SERVER=" + server + ";" +
"DATABASE=" + database + ";" +
"User Id=" + uid + ";" +
"PASSWORD=" + password + ";" +
"PORT=" + port + ";" +
"Connect Timeout=240";
// string connectionString = "server=localhost;User Id=test;database=eagri;port=13306";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server, access denied.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
Console.WriteLine("Cannot connect to server. Access Denied");
break;
case 1045:
Console.WriteLine("Invalid username/password, please try again");
break;
default:
Console.WriteLine("Unable to connect to mysql server, unknown error {0}", ex.ToString());
break;
}
return false;
}
}
//Close connection
public bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
//Insert statement
public void Insert(string query)
{
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
//Update statement
public void Update(string query)
{
//Open connection
if (this.OpenConnection() == true)
{
//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = query;
//Assign the connection using Connection
cmd.Connection = connection;
//Execute query
int i = cmd.ExecuteNonQuery();
Console.WriteLine("DB update result: {0} updated", i);
//close connection
this.CloseConnection();
}
}
//Delete statement
public void Delete(string query)
{
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
//Select statement
public MySqlDataReader Select(string query)
{
MySqlDataReader dataReader = null;
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
dataReader = cmd.ExecuteReader();
}
return dataReader;
}
//Count statement
public int Count(string query)
{
int Count = -1;
//Open Connection
if (this.OpenConnection() == true)
{
//Create Mysql Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//ExecuteScalar will return one value
Count = int.Parse(cmd.ExecuteScalar() + "");
//close Connection
this.CloseConnection();
}
return Count;
}
//Backup
public void Backup()
{
}
//Restore
public void Restore()
{
}
}
}
答案 0 :(得分:0)
connection = DBServer;
port = DBport;
database = DBdatabase;
etc...
但请记住,您的应用程序在应用程序加载时加载Configigurationfile一次,因此如果更改运行时,需要强制更新它以获取新值 Change appconfig on runtime