从 XML 文件获取连接字符串的一部分
本文关键字:字符串 一部分 连接 获取 XML 文件 | 更新日期: 2023-09-27 18:37:12
我试图根据一些包含服务器IP地址的XML文件构建一个与火鸟数据库的连接字符串。
public class globals
{
static string _serv;
public static string serv
{
get
{
return _serv;
}
set
{
_serv=value;
}
}
public static XmlDocument xmld = new XmlDocument();
public static void serv_check()
{
xmld.Load("ustawienia.xml");
globals._serv = xmld.GetElementsByTagName("ip").Item(0).InnerText;
if (globals._serv.Length<=0)
{
MessageBox.Show("Nie zdefiniowano servera");
Server_Deff sdf = new Server_Deff();
sdf.ShowDialog();
}
}
static string db_user = "SYSDBA";
static string db_pass = "masterkey";
public static string cstr=
"User=" + globals.db_user + ";" +
"Password=" + globals.db_pass + ";" +
"Database=db_kanc;" +
"DataSource=" + globals.serv +
"Port=3050;" +
"Dialect=3;" +
"Charset=NONE;" +
"Role=appka;" +
"Connection lifetime=15;" +
"Pooling=true;" +
"MinPoolSize=0;" +
"MaxPoolSize=50;" +
"Packet Size=8192;" +
"ServerType=0";
public static FbConnection conn = new FbConnection(globals.cstr);
public static FbTransaction transaction;
public static void openConnection() // Open database Connection
{
conn.Open();
transaction = conn.BeginTransaction();
}
由于某些原因,无论是在 XML 文件中定义 IP 地址还是当它不是变量服务时,似乎都不是空的。我可以通过消息框获取值,但不能作为连接字符串中的正确数据源。
尝试连接到数据库时,我收到如下错误:
FirebirdSql.Data.FirebirdClient.FbException: Unable to complete network request to host "Port=3050". ---> FirebirdSql.Data.Common.IscException: Unable to complete network request to host "Port=3050".
w FirebirdSql.Data.Client.Managed.Version10.GdsConnection.Connect()
w FirebirdSql.Data.FirebirdClient.ClientFactory.CreateManagedDatabase(FbConnectionString options)
w FirebirdSql.Data.FirebirdClient.ClientFactory.CreateDatabase(FbConnectionString options)
w FirebirdSql.Data.FirebirdClient.FbConnectionInternal.Connect()
有什么想法吗?
错误
Unable to complete network request to host "Port=3050"
CSTR 的段
"DataSource=" + globals.serv +
"Port=3050;" +
这意味着 globals.serv 只是一个空字符串。根本原因是 serv_check() 在 cstr 初始化且 cstr 未更新后运行。
如果你需要更改 globals.serv,你还需要更新 cstr 或只是创建一个新的只读属性(注意添加数据源后的";")
public static string ConnectionString
{
get
{
return "User=" + globals.db_user + ";" +
"Password=" + globals.db_pass + ";" +
"Database=db_kanc;" +
"DataSource=" + globals.serv + ";" +
"Port=3050;" +
"Dialect=3;" +
"Charset=NONE;" +
"Role=appka;" +
"Connection lifetime=15;" +
"Pooling=true;" +
"MinPoolSize=0;" +
"MaxPoolSize=50;" +
"Packet Size=8192;" +
"ServerType=0";
}
}
试试这个:
public class globals
{
public static string serv { get; set; }
// other variables here
}
public static XmlDocument xmld = new XmlDocument();
public static void serv_check()
{
xmld.Load("ustawienia.xml");
globals._serv = xmld.GetElementsByTagName("ip").Item(0).InnerText;
if (globals._serv.Length<=0)
{
MessageBox.Show("Nie zdefiniowano servera");
Server_Deff sdf = new Server_Deff();
sdf.ShowDialog();
}
}
static string db_user = "SYSDBA";
static string db_pass = "masterkey";
public static string cstr = string.Format(
"User={0};Password={1};Database=db_kanc;DataSource={2};Port=3050;" +
"Dialect=3;Charset=NONE;Role=appka;Connection lifetime=15;" +
"Pooling=true;MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;" +
"ServerType=0;",
globals.db_user,
globals.db_pass,
globals.serv);
public static FbConnection conn = new FbConnection(globals.cstr);
public static FbTransaction transaction;
public static void openConnection() // Open database Connection
{
conn.Open();
transaction = conn.BeginTransaction();
}
问题解决了。
public class globals
{
static string db_user = "SYSDBA";
static string db_pass = "masterkey";
public static string host;
public static XmlDocument xmld = new XmlDocument();
public static string serv_check()
{
xmld.Load("ustawienia.xml");
host = xmld.GetElementsByTagName("ip").Item(0).InnerText;
if (host.Length<=0)
{
MessageBox.Show("Nie zdefiniowano servera");
Server_Deff sdf = new Server_Deff();
sdf.ShowDialog();
}
return host;
}
public static string serv=serv_check().ToString();
public static string cstr
{
get
{ return
"User=" + globals.db_user + ";" +
"Password=" + globals.db_pass + ";" +
"Database=db_kanc;" +
"DataSource=" + globals.serv + ";" +
"Port=3050;" +
"Dialect=3;" +
"Charset=NONE;" +
"Role=appka;" +
"Connection lifetime=15;" +
"Pooling=true;" +
"MinPoolSize=0;" +
"MaxPoolSize=50;" +
"Packet Size=8192;" +
"ServerType=0";
}
}
public static FbConnection conn = new FbConnection(globals.cstr);
public static FbTransaction transaction;
public static void openConnection() // Open database Connection
{
conn.Open();
transaction = conn.BeginTransaction();
}
public static void closeConnection() // database connection close
{
transaction.Commit();
conn.Close();
}
public static int user_id = new int();
public static int user_r = new int();
}
也许不是最纯粹的解决方案,但对我有用:)谢谢埃里克!