我有嘗試使用含有通配符的字符串匹配的數據表的列名的問題。不匹配的數據表列c#通配符值
我有不同的列名的數據表,其中包括一個名爲像集如下:「PSA」,「公安局」,「PSC」,等。我想遍歷標題中包含Ps的所有列,並使用這些標題來提取數據。
目前,我有以下代碼未能找到任何匹配。我在if語句(「PsA」)中替換了一個直接值作爲測試,它工作正常;但是,當我使用通配符時,我找不到匹配項。我也試過Regex沒有運氣。
private void dfaSection_SelectedIndexChanged(object sender, EventArgs e)
{
string psText = null;
string colID = "Ps*";
offBox.Text = ""; //Clear textbox if a reselection occurs
psBox.Text = ""; //Clear textbox if a reselection occurs
info.offTitle = dfaSection.Text; //Set textbox from variable
dt = opr.findOffByTitle(info); //Get datatable from SQL database
if(dt.Rows.Count > 0)
{
offBox.Text = dt.Rows[0][5].ToString(); //Set textbox from datatable
foreach(DataColumn dc in dt.Columns) //Loop through datatable columns
{
if (dc.ColumnName.ToString() == colID) //Check that column title matches test string - Later addition--> && dt.Rows[0][dc].ToString() != null)
{
psText = dt.Rows[0][dc].ToString() + "\n\n"; //Add data from matched column to string variable
}
}
psBox.Text = psText; //Set textbox from variable
}
}
編輯:問題使用.Contains(colID)
列名正在匹配,串固定,現在沒有被加載到psText
變量,但我會花一些時間與得到它的工作。謝謝斯卡羅斯伊利亞斯。
'我自己也嘗試用正則表達式不luck.'請告訴我們的代碼。 – mjwills