我正在嘗試遍歷數據集的行和列以搜索數據集的name
列 - 和來自DataReader
對象的ColumnName
之間的匹配項。當前上下文中不存在名稱'colType'
我有一個名爲RECORDS
的新表,它在程序啓動時爲空。我還有一個名爲ColumnPositions
的預填充表,其中有RECORDS
表中的子列名。此例程旨在顯示所有可用列的子集 - 作爲默認顯示樣式。
我的代碼工作...除了獲得dr["type"]
值的代碼行外。我得到的錯誤:
The name '
colType
' does not exist in the current context.
正如你可以清楚地看到,我的字符串變量是在WHILE和FOREACH循環之外聲明的。行聲明colName =
工作得很好。但colType
每次都會失敗。如果我在VS2010的中級窗口中進行語句檢查? dr["type"]"
,我會得到結果integer
。但是當我檢查? colType
時,我收到上面提到的錯誤消息。
DataRow對象dr
的智能感知揭示了一個包含6個項目的數組。數組中的索引1映射到name
。索引2映射到type
。當我在中級窗口中檢查? dr[2]
的值時,相同的結果會返回integer
。這是對的。但是,無論何時將此值分配給colType
,VS2010都會抱怨。
我不是C#的新手,所以在發佈之前我做了很多測試和谷歌搜索。我希望這是我沒有看到樹林裏的森林。
這裏是我的代碼:
// get table information for RECORDS
SQLiteCommand tableInfo = new SQLiteCommand("PRAGMA table_info(Records)", m_cnCaseFile);
SQLiteDataAdapter adapter = new SQLiteDataAdapter(tableInfo);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable dt = ds.Tables[0];
SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM ColumnPositions WHERE ColumnStyle_ID = " + styleID + " ORDER BY ColumnPosition_ID ASC", m_cnCaseFile);
SQLiteDataReader colReader = cmd.ExecuteReader();
string colName = "";
string colType = "";
if (dt != null && colReader.HasRows)
{
while (colReader.Read())
{
foreach(DataRow dr in dt.Rows)
{
colType = Convert.ToString(dr["type"]);
colName = dr["name"].ToString();
if (colReader["ColumnName"].ToString() == colName)
{
DataGridViewColumn dgvCol = new DataGridViewColumn();
}
}
}
}
dt.Dispose();
colReader.Close();
我也應該簡要介紹一下例程的目的。我有一個名爲RECORDS的新表,它在程序啓動時是空的。我還有一個名爲ColumnPositions的預填充表,其中有一個在RECORDS表中找到的列名的子集。此例程旨在顯示所有可用列的子集 - 作爲默認顯示樣式。真正的編碼將被插入定義新的DataGridViewColumn()的代碼行中。但現在,這個功能非常有效地說明了這項任務。 – 2013-03-26 02:21:03
這是您遇到的運行時錯誤?不是編譯器錯誤? – 2013-03-26 02:29:27
是的。這是運行時錯誤。 – 2013-03-26 13:53:45