Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't see how this could not have thrown an <code>SqlException</code>; for two reasons: </p> <ol> <li><p>The SQL you have written does not seem right and </p></li> <li><p>If it was right it seems you would be expecting mulitple restults (using <code>*</code>), in this case using <code>while (reader.Read()) { ... }</code> will loop through each result and set the logic accordingly. So, if the first user you had was <code>USER</code> and the second <code>Administrator</code>, the button enabled state would be set according to the last accessed value - not what you want.</p></li> </ol> <p>Instead, set up a static utilities class to help with SQL queries. So you would have some thing like</p> <pre><code>public static calss Utilities { public static bool SQLResult(SqlConnection conn, string strSQL) { try { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = strSQL; strResult = cmd.ExecuteScalar().ToString(); } catch (Exception e) { // Return error or display. return false; } return true; } } </code></pre> <p>where this method will expect to return a single text result. </p> <p>Now the SQL you want to use is </p> <pre><code>SELECT SYSTEM_USER; GO </code></pre> <p>This will return the current logged in user; the user executing the query. For me it returns 'ServerName\Administrator'. So your logic could be </p> <pre><code>string sql = "SELECT SYSTEM_USER;"; string result = Utilities.SQLResult(someValidConnection, sql); if (result.Contains("Administrator")) // Do stuff. </code></pre> <p>I hope this helps.</p> <p><em>Note. The above does not consider SQL Injection attacks or other security issues. For most basic WinForm applications (non-webbased) these security issues are IMO not worth worrying about.</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload