我有两种访问本地数据库的方法。除了一个访问一个表和另一个表外,它们是相同的。表格是一样的。连接字符串未正确初始化VSTO插件
这里是工作的代码:
public void populateClientDict(SqlConnection conn)
{
Dictionary<string, string> clientDict = new Dictionary<string, string>();
try
{
using (conn)
{
SqlCommand command = new SqlCommand(
@"SELECT ClientDirName, ClientEmailDomain FROM ClientTable;",
conn);
conn.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string clientDir = reader.GetString(0);
string clientEmail = reader.GetString(1);
clientDict.Add(clientEmail, clientDir);
}
}
else
{
MessageBox.Show("No rows found in ClientTable", "Rows Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
reader.Close();
}
}
catch (InvalidOperationException ex)
{
MessageBox.Show(String.Format("Exception while accessing ClientTable: {0}", ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (SqlException ex)
{
MessageBox.Show(String.Format("Exception while accessing ClientTable: {0}", ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
这里是什么康恩说法是相当于两个功能:SqlConnection conn = new SqlConnection(connString);
和CONNSTRING是private string connString = @"Server=.;User Id=cshenkan; Password=$henkan72;Database=ArchiveAddin";
不工作的代码(称为直接在第一功能后):
public void populateClientHistoryDict(SqlConnection conn)
{
Dictionary<string, string> clientHistoryDict = new Dictionary<string, string>();
try
{
using (conn)
{
SqlCommand command = new SqlCommand(
@"SELECT ClientDirName, ClientEmailDomain FROM ClientHistoryTable;",
conn);
conn.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string clientDir = reader.GetString(0);
string clientEmail = reader.GetString(1);
clientHistoryDict.Add(clientEmail, clientDir);
}
}
else
{
MessageBox.Show("No rows found in ClientHistoryTable", "Rows Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
reader.Close();
}
}
catch (InvalidOperationException ex)
{
MessageBox.Show(String.Format("Exception while accessing ClientHistoryTable: {0}", ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (SqlException ex)
{
MessageBox.Show(String.Format("Exception while accessing ClientHistoryTable: {0}", ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
的例外,我得到的是:
访问ClientHistoryTable时出现异常:System.InvalidOperationException:ConnectionString属性尚未初始化。 (我在第二个函数中调用conn.Open(););
另外对于参考我有另一个函数,我连接到看看它是否存在。它适用于ClientTable,但不适用ClientHistoryTable。为什么会这样呢?它们是完全一样的。
任何想法?我已经处理过错误,但从未在它工作时我不知道为什么我不会在第二次尝试到达一个表而不是第一个时出现这个错误。
谢谢
编辑:
奇怪的是,如果我把SqlConnection con = new SqlConnection(connString)
里面populateClientHistoryDict()
它的作品。我对populateClientDict()
做了同样的工作,它也工作得很好,但它已经做到了。为什么如果我将这两个函数的连接传递给它,它会起作用,但如果我在函数内部实例化连接,它会起作用吗?一个SqlConnection只能用于数据库的一次访问?我在每个功能附加在连接字符串违反干,但我不能让它使用此代码的工作:
SqlConnection conn = new SqlConnection(connString);
if (checkDatabaseExists())
{
populateClientDict(conn);
populateClientHistoryDict(conn);
}
第一功能的工作原理,但不是第二。正如我所说,如果我删除参数,并把连接对象内的函数,它的工作正常。为什么是这样?我对每个访问的一个SqlConnection正确吗?如果我添加第二个SqlConnection conn2 = new SqlConnection(connString);
,然后将它传递给populateClientHistoryDict(conn2)
它再次工作,似乎我是正确的每个使用一个连接对象。那是对的吗?还是我误会了?
这里是工作的代码调用这些功能:
SqlConnection conn = new SqlConnection(connString);
SqlConnection conn2 = new SqlConnection(connString);
if (checkDatabaseExists())
{
populateClientDict(conn);
populateClientHistoryDict(conn2);
}
看来我可能已经回答了我自己的问题,但反馈仍然有很大的帮助,我还是会接受任何好的答案是重申我说了什么并详细阐述一下,或者纠正我所说的并且详细阐述一下。
另外我也可以问,这是一个巨大的交易我没有使用连接字符串的应用程序设置xml和引用他们与ConfigurationManager?我只有一个数据库连接到,只有两次。似乎没有必要。尽管我将通过Form完成两个表格的CRUD操作,但这是另一回事。
感谢您的耐心等待。