【发布时间】:2016-07-26 22:05:45
【问题描述】:
我从本地数据库的属性中复制了 ConnectString 值。
属性中的连接字符串是:
Data Source=Cyber\SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True;
当我直接将 ConnectionString 复制到 ConnectString 时,我得到一个错误。所以我把那个“\”去掉,我没有收到错误。但是它仍然不起作用。我还注意到人们通常将 ConnectionString 值更改为单个单词以使其变得容易。但是,我的 VB 属性部分不允许我更改它。 Here is the error I get
public class SQLConnection
{
#region MemberVariables
private SqlConnection mConnection = null;
private SqlDataAdapter mDataAdapter = null;
private SqlCommand mCommand = null;
static string mDbConnString = string.Empty;
#endregion
#region PublicMemberVariables
public SqlConnection Connection
{
get
{
return mConnection;
}
set
{
mConnection = value;
}
}
public SqlDataAdapter DataAdapter
{
get
{
return mDataAdapter;
}
set
{
mDataAdapter = value;
}
}
public SqlCommand Command
{
get
{
return mCommand;
}
set
{
mCommand = value;
}
}
public string ConnectString
{
get
{
return mDbConnString;
}
set
{
lock (mDbConnString)
{
mDbConnString = value;
}
lock (mConnection)
{
mConnection.ConnectionString = mDbConnString;
}
}
}
#endregion
public void TestConnection()
{
ConnectString = "Data Source=Cyber SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True";
Connection = new SqlConnection(ConnectString);
Connection.Open();
MessageBox.Show(Connection.State.ToString());
}
}
【问题讨论】:
-
如果您打算只使用一个连接来访问所有数据库,那您就错了。正确的做法是:打开连接,做操作,关闭连接,处理连接。
-
看起来你正在构建的数据库“助手”类通常比它们的价值都多。
-
@AndrewMorton 谢谢。我想要实现的是将 xml 文件转换为数据库。我正在考虑使用一个连接。但仍在学习并尝试制定策略。
-
也有人回答了这个问题并删除了他们的答案。如果有人在未来遇到同样的问题,答案是:我必须使用“\\”而不是“\”。
-
@JackTheRipper 请注意您的问题与实际问题无关。也许你需要做更多的研究;)How do I ask a good question?
标签: c# sql database connection-string