Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That's on the right track, but you're missing a couple important points:</p> <ol> <li>You aren't closing your connection correctly. If your sql statement throws an exception, you'll never reach the .Close() call. Depending on your database type and how it's configured, this can result in maxing out your db server's available connections and effectively locking yourself out of your own database.</li> <li>Where did the "1" come from in "idPeople=1"? There is a right way and a wrong way to build queries containing argument data like that "1". Make sure you do it the right way.</li> <li>"SELECT *" is considered bad form... much better to specify the column list.</li> </ol> <p>Here is an example that solves all these issues:</p> <pre><code>//often this will loaded from a config file string connString = "Server=localhost;database=super;uid=root;password=root"; string sql = "SELECT &lt;column list&gt; FROM people WHERE idPeople= @idPeople"; using (var cn = new MySqlConnection(connString)) uinsg (var cmd = new MySqlCommand(sql, cn)) { cmd.Parameters.Add("@idPeople", MySqlDbType.Int32).Value = 1; conn.Open(); //get data } </code></pre> <p>You'll see some people tell you to use an ORM, like NHibernate, linq-to-sql, Entity Framework, etc. I tend to think there's still a place for hand-coded and tuned sql, and I don't pay much attention to them. A light-weight data access framework like <a href="https://code.google.com/p/dapper-dot-net/" rel="nofollow noreferrer">Dapper</a> can be helpful, though. These can abstract away much of the boilerplate code not only from what I just wrote, but also the business and transport object layers, and still let you write whatever sql you need.</p>
    singulars
    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. VO
      singulars
      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