2012-12-08 21 views
0

我使用的訪問表SQL如何從一個單元格的值在訪問表

我使用這個oleDBManager包括這些公共職能:

/// <summary> 
/// Excecutes an SELECT query and returns the data. 
/// </summary> 
/// <param name="query">the query string</param> 
/// <returns>returns an DataTable instance with the recived data from the selection query.</returns> 
public DataTable ExcecuteRead(string query) { 
    this.link.Open(); 
    // --- 
    this.dataAdapter = new OleDbDataAdapter(query, this.link); 
    // --- 
    this.dataTable = new DataTable(); 
    this.dataAdapter.Fill(this.dataTable); 
    // --- 
    this.link.Close(); 
    // --- 
    return this.dataTable; 
} 

/// <summary> 
/// Returns an HTML table code, with all the rows and the values of the results. 
/// </summary> 
/// <param name="query">the query string</param> 
/// <returns>returns an HTML code as a string</returns> 
public string ExcecuteTableRead(string query) 
{ 
    string output = "<table border=\"1\">"; 
    // --- 
    this.dataTable = this.ExcecuteRead(query); 
    // --- 
    foreach (DataRow row in this.dataTable.Rows) 
    { 
     output += "<tr>"; 
     // --- 
     foreach (object obj in row.ItemArray) 
     { 
      output += "<td>" + obj.ToString() + "</td>"; 
     } 
     // --- 
     output += "</tr>"; 
    } 
    // --- 
    output += "</table>"; 
    // --- 
    return output; 
} 
/// <summary> 
/// Returns an HTML table code, with all the rows and the values of the results. 
/// </summary> 
/// <param name="query">the query string</param> 
/// <param name="max">the maximum number of rows to show</param> 
/// <returns>returns an HTML code as a string</returns> 
public string ExcecuteTableRead(string query, int max) 
{ 
    int i = 0; 
    string output = "<table border=\"1\">"; 
    // --- 
    this.dataTable = this.ExcecuteRead(query); 
    // --- 
    foreach (DataRow row in this.dataTable.Rows) 
    { 
     if (i < max) 
     { 
      output += "<tr>"; 
      // --- 
      foreach (object obj in row.ItemArray) 
      { 
       output += "<td>" + obj.ToString() + "</td>"; 
      } 
      // --- 
      output += "</tr>"; 
     } 
     i++; 
    } 
    // --- 
    output += "</table>"; 
    // --- 
    return output; 
} 

在我的「用戶「表,我有一個」用戶名「,」用戶名「,」密碼「和」登錄「爲每個用戶。 我的問題是,當用戶登錄(我有用戶名和密碼)時,如何獲取他的「登錄」列的值? 如果我可以將它設置爲一個int,會更好(如果它很重要,我已設置「登錄」列從「文本」中訪問'數字')

編輯:我正在嘗試要做的是更新的時間在用戶登錄次數。如果有一個更好的辦法,請告訴我。

+0

所以基本上我想,該SQL語法應該是 '「SELECT FROM用戶登錄WHERE用戶名='」 +用戶名+「 '''' 但問題是,當我使用ExcecuteRead()fu它返回一個DataTable類型,而不是一個int或一個字符串。 有幫助嗎? –

回答

0

答案:

所以,儘管沒有人回答我,我已經成功地學習它自己:

這裏是代碼的整個部分(用戶名是用戶的名稱,也是一個它具有相同的名稱的字符串,登錄是列,用戶是表)

string logins = db.ExcecuteTableRead("SELECT logins FROM users WHERE username='" + username + "'"); 
logins = logins.Substring(21, 20); 
logins = Regex.Match(logins, @"\d+").Value; 
int loginsInt = Int32.Parse(logins) + 1; 
int a = db.ExecuteQuery("UPDATE users SET logins='" + loginsInt.ToString() + "' WHERE username='" + username + "'"); 
0

你可以使用的ExecuteScalar()函數返回的對象 OR 你可以使用ExecuteRead()和
將其存儲爲像這樣的字符串

string logins= dt.rows[0].itemarray[0].tostring() 
相關問題