我有这个在类中创建 SqlConnection 字符串的方法。我从 .txt 文件中读取数据,但问题是如果没有插入数据,应用程序甚至都不会打开。我想知道如何在消息框中引发异常并将该命令直接插入到此类方法中。我不确定这是否可能。对不起,如果我的回答太琐碎或毫无意义。我很久没有编程了。
internal static class DataSource
{
private static string _ConnectionString;
public static string ConnectionString
{
get
{
if (_ConnectionString == null)
_ConnectionString = FunctionToDynamicallyCreateConnectionstring();
return _ConnectionString;
}
}
private static string FunctionToDynamicallyCreateConnectionstring()
{
string path = "C:\\Users\\marek\\Documents\\Visual Studio 2012\\Projects\\tours\\tours\\sql_string.txt";
StreamReader sr = new StreamReader(File.Open(path, FileMode.Open));
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();
cb.DataSource = sr.ReadLine();
cb.InitialCatalog = sr.ReadLine();
cb.UserID = sr.ReadLine();
cb.Password = sr.ReadLine();
return cb.ToString();
}