LINQ SQL 从 TXT 文件读取时连接字符串不起作用
LINQ SQL Connection String Not Working When Read From TXT File
我已经完成了应用程序的构建。
我列表中的最后一件事是将 SQL 连接字符串放在一个文件中,而不是硬编码(以便用户可以在需要时对其进行编辑)。
连接失败,我从 DataContext 收到异常。
连接字符串绝对正确。由于一切正常,唯一改变的是我将连接字符串放在 txt 文件中,而不是对其进行硬编码。
之前(我的应用程序连接到数据库正常):
private string conString = "Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True";
public LogIn()
{
dc = new MyDataContext(conString);
if (dc.DatabaseExists())
{
InitializeComponent();
}
else
{
MessageBox.Show("There has been an error, please try again. If ther problem persists, call you know who. Error: Can't connect to database! Make sure the network is all good (connection to ADMIN PC)" /*+ e.ToString()*/);
}
}
现在(不工作):
private string conString;
public LogIn()
{
try
{
ConnectionString.globalConString = System.IO.File.ReadAllText("connString.txt").ToString();
this.conString = ConnectionString.globalConString;
}
catch (IOException e)
{
MessageBox.Show("There was an error reading the conn string file.");
return;
}
dc = new MyDataContext(conString);
if (dc.DatabaseExists())
{
InitializeComponent();
}
else
{
MessageBox.Show("There has been an error, please try again. If ther problem persists, call you know who. Error: Can't connect to database! Make sure the network is all good (connection to ADMIN PC)" /*+ e.ToString()*/);
}
}
txt 文件仅包含:
Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True
文件在正确的位置,应用程序可以读取它(我已经用 MessageBox.Show() 输出了字符串)。
我能想到的是这个字符串实际上不是字符串而是某种奇怪的格式?我不知道。
顺便说一句,尝试连接的位抛出异常(不是文件读取位 - 代码读取文件,但 DataContext 不喜欢传递给它的字符串)。
有什么想法吗??
谢谢
您不需要对文本文件中的反斜杠进行转义,只需在代码中即可。该文件应包含:
Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True
尝试删除 HP
之后的双 \
:
Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True
双反斜杠在定义 C# 字符串时有效,因为您需要转义反斜杠字符,但从文件中读取时,它会被视为两个反斜杠字符。
你不需要放双反斜杠试试
Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True
我已经完成了应用程序的构建。
我列表中的最后一件事是将 SQL 连接字符串放在一个文件中,而不是硬编码(以便用户可以在需要时对其进行编辑)。
连接失败,我从 DataContext 收到异常。
连接字符串绝对正确。由于一切正常,唯一改变的是我将连接字符串放在 txt 文件中,而不是对其进行硬编码。
之前(我的应用程序连接到数据库正常):
private string conString = "Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True";
public LogIn()
{
dc = new MyDataContext(conString);
if (dc.DatabaseExists())
{
InitializeComponent();
}
else
{
MessageBox.Show("There has been an error, please try again. If ther problem persists, call you know who. Error: Can't connect to database! Make sure the network is all good (connection to ADMIN PC)" /*+ e.ToString()*/);
}
}
现在(不工作):
private string conString;
public LogIn()
{
try
{
ConnectionString.globalConString = System.IO.File.ReadAllText("connString.txt").ToString();
this.conString = ConnectionString.globalConString;
}
catch (IOException e)
{
MessageBox.Show("There was an error reading the conn string file.");
return;
}
dc = new MyDataContext(conString);
if (dc.DatabaseExists())
{
InitializeComponent();
}
else
{
MessageBox.Show("There has been an error, please try again. If ther problem persists, call you know who. Error: Can't connect to database! Make sure the network is all good (connection to ADMIN PC)" /*+ e.ToString()*/);
}
}
txt 文件仅包含:
Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True
文件在正确的位置,应用程序可以读取它(我已经用 MessageBox.Show() 输出了字符串)。
我能想到的是这个字符串实际上不是字符串而是某种奇怪的格式?我不知道。
顺便说一句,尝试连接的位抛出异常(不是文件读取位 - 代码读取文件,但 DataContext 不喜欢传递给它的字符串)。
有什么想法吗??
谢谢
您不需要对文本文件中的反斜杠进行转义,只需在代码中即可。该文件应包含:
Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True
尝试删除 HP
之后的双 \
:
Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True
双反斜杠在定义 C# 字符串时有效,因为您需要转义反斜杠字符,但从文件中读取时,它会被视为两个反斜杠字符。
你不需要放双反斜杠试试
Data Source=HP\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True