UPDATE: I greatly improved this connection class in this post, read: http://weblog.simondeshaies.net/post/2008/11/How-To-Connect-to-SQL-Server-with-C-and-Optimize-Reusability.aspx
I started a thread on the asp.net forums a while back wanting to share with the others a easy way to connect to SQL. Since then I perfected quite a bit the code and made it a class.cs file, that can be up and running inside a few minutes.
Connecting to SQL using the design view in visual studio is quite easy and straight forward. I found to be somewhat limited though. Having a class that you can invoke in all your application and in other classes is the works.
Here I demonstrate how to use the C# in a class file to connect to SQL, and INSERT, SELECT or UPDATE an existing SQL database. In this example I will be using the Microsoft SQL Express Server. Also the connection string in this example is for a local server with SQL security; if you need to connect to an external server or any other king of SQL server go to http://connectionstrings.com to get the replacement connection string code.
First off unlike asp.net insert you don’t use a connection string in the web.config, so no changes need to be done there.
The class itself goes like this, you can grab the sample solution and try it out.
In App_Data MySqlConnection.cs add the following code.
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
/// <summary>
/// This Class is specifically design to connect with optimized pooling to MyDatabase.
///
/// In private string mySqlConnectionString MyDatabase is the database you want to connect to.
/// MyUser the sql autentication user and MyPassword the Password...
/// </summary>
public class MySqlConnection
{
public SqlConnection Connection;
public SqlDataReader DataReader;
public SqlCommand Command;
private string mySqlConnectionString = "server=.\\SQLExpress;database=MyDatabase;User ID=MyUser;Password=MyPassword;Trusted_Connection=True;";
public void CloseConn()
{
if (Connection != null)
{
if (Connection.State == ConnectionState.Open)
{
Connection.Close();
}
Connection.Dispose();
}
}
public SqlConnection CreateConn()
{
if (Connection == null) { Connection = new SqlConnection(); };
if (Connection.ConnectionString == string.Empty || Connection.ConnectionString == null)
{
try
{
Connection.ConnectionString = "Min Pool Size=5;Max Pool Size=40;Connect Timeout=4;" + mySqlConnectionString + ";";
Connection.Open();
}
catch (Exception)
{
if (Connection.State != ConnectionState.Closed)
{
Connection.Close();
}
Connection.ConnectionString = "Pooling=false;Connect Timeout=45;" + mySqlConnectionString + ";";
Connection.Open();
}
return Connection;
}
//SqlConnection Connection = new SqlConnection();
if (Connection.State != ConnectionState.Open)
{
try
{
Connection.ConnectionString = "Min Pool Size=5;Max Pool Size=40;Connect Timeout=4;" + mySqlConnectionString + ";";
Connection.Open();
}
catch (Exception)
{
if (Connection.State != ConnectionState.Closed)
{
Connection.Close();
}
Connection.ConnectionString = "Pooling=false;Connect Timeout=45;" + mySqlConnectionString + ";";
Connection.Open();
}
}
return Connection;
}
public MySqlConnection()
{
}
}
Here you go, have plenty of fun and let me know what you think.