私の用語がオフの場合、私はC#を初めて使用しています。私はdbname、ユーザー名、パスワードのようなmysqlのconn値を格納するためにApplicationContextファイルを使用しようとしています。 mysqlのconn文字列を持つクラスは、ApplicationContextの名前空間を「使用」していますが、接続文字列を出力すると、値によってその文字列が作成されます。C#ApplicationContextの使用
友人は「私はそれを初期化していません」と言っていましたが、「それ」が何であったかを拡大することはできませんでした。
と "Console.WriteLine(" 1 ");" ApplicationContext.csには決して表示されません。 ApplicationContextオブジェクトとそのオブジェクトのInitialize()コールを作成する必要がありますか?
ありがとうございました。
ApplicationContext.cs:
namespace NewApplication.Context
{
class ApplicationContext
{
public static string serverName;
public static string username;
public static string password;
public static void Initialize()
{
//need to read through config here
try
{
Console.WriteLine("1");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(".\\Settings.xml");
XmlNodeList serverNodeList = xDoc.GetElementsByTagName("DatabaseServer");
XmlNodeList usernameNodeList = xDoc.GetElementsByTagName("UserName");
XmlNodeList passwordNodeList = xDoc.GetElementsByTagName("Password");
}
catch (Exception ex)
{
// MessageBox.Show(ex.ToString());
//TODO: Future write to log file
username = "user";
password = "password";
serverName = "localhost";
}
}
}
}
MySQLManager.cs: 注:コード内で表示されますようdbnameはユーザー名と同じですが、私はそれをして友人からこれをコピーしました。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using NewApplication.Context;
namespace NewApplication.DAO
{
class MySQLManager
{
private static MySqlConnection conn;
public static MySqlConnection getConnection()
{
if (conn == null || conn.State == System.Data.ConnectionState.Closed)
{
string connStr = "server=" + ApplicationContext.serverName +
";user=" + ApplicationContext.username + ";database=" + ApplicationContext.username + ";port=3306;password=" +
ApplicationContext.password + ";";
conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL... ");
Console.WriteLine("Connection string: " + connStr + "\n");
conn.Open();
// Perform databse operations
// conn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
return conn;
}
}
}
と、まだ読んでくれてありがとう、これは以前の2つのファイルを使用するコードです:
class LogDAO
{
MySqlConnection conn;
public LogDAO()
{
conn = MySQLManager.getConnection();
}
はrd42
既にApplicationContextクラスがあります...あなたはあなたのもので何をしようとしていますか? http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx –
ありがとう、私はそれを調べます。 – rd42