Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the complete code of a <code>SqlHelper</code> that I'd used on some small projects. But carefull with <code>static</code> for this kind of class. If you will use it for Web project, remember that the connection will be shared at the same instance for all users, which can cause bad problems...</p> <pre><code>using System.Data; using System.Data.SqlClient; using System.Web.Configuration; public class SqlHelper { private SqlConnection connection; public SqlHelper() { connection = new SqlConnection(); } public void OpenConnection() { // Updated code getting the ConnectionString without hard naming it. // Yes, if you have more than 1 you'll have problems... But, how many times it happens? if (WebConfigurationManager.ConnectionStrings.Length == 0) throw new ArgumentNullException("You need to configure the ConnectionString on your Web.config."); else { connection.ConnectionString = WebConfigurationManager.ConnectionStrings[0].ConnectionString; connection.Open(); } } public void CloseConnection() { if (connection != null &amp;&amp; connection.State != ConnectionState.Closed) connection.Close(); } public DataTable ExecuteToDataTable(string sql) { DataTable data; SqlCommand command = null; SqlDataAdapter adapter = null; try { if (connection.State != ConnectionState.Open) OpenConnection(); command = new SqlCommand(sql, connection); adapter = new SqlDataAdapter(command); retorno = new DataTable(); adapter.Fill(data); } finally { if (command != null) command.Dispose(); if (adapter != null) adapter.Dispose(); CloseConnection(); } return data; } public int ExecuteNonQuery(string sql) { SqlCommand command = null; try { if (connection.State != ConnectionState.Open) OpenConnection(); command = new SqlCommand(sql, connection); return command.ExecuteNonQuery(); } finally { if (command != null) command.Dispose(); CloseConnection(); } } public object ExecuteScalar(string sql) { SqlCommand command = null; try { if (connection.State != ConnectionState.Open) OpenConnection(); command = new SqlCommand(sql, connection); return command.ExecuteScalar(); } finally { if (command != null) command.Dispose(); CloseConnection(); } } } </code></pre> <p>Sample usage:</p> <pre><code>SqlHelper sql = new SqlHelper(); DataTable data = sql.ExecuteToDataTable("SELECT * FROM Customers"); int affected = sql.ExecuteNonQuery("INSERT Customers VALUES ('Test')"); </code></pre> <p>But if you really want <code>static</code> (if you is on a single user enviroment), just put <code>static</code> on all methods.</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. 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