using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class sql_connexion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		// initialisation de la connexion
        string c_string = ConfigurationManager.ConnectionStrings["bancaConnectionString"].ConnectionString;
		SqlConnection cx_annuaire;
		cx_annuaire=new SqlConnection();
		cx_annuaire.ConnectionString=c_string;

		// ouverture
		cx_annuaire.Open();

		// opérations SQL 
		string rq = "select count(*) from utilisateur";
		SqlCommand sql;
		sql = new SqlCommand();
		sql.CommandText = rq;
		sql.CommandType = CommandType.Text; // valeur par défaut
		sql.Connection = cx_annuaire;

		int cu = (int)sql.ExecuteScalar();		
		Label1.Text = string.Format("Nombre d'utilisateur : {0}", cu);
		
		// fermeture
		cx_annuaire.Close();
    }

	void connexion_sure()
	{
		// initialisation de la connexion		                                          
		string c_string = @"data source=.\SQL2019; initial catalog=annuaire; integrated security=false; user id=saw; password=password";
		SqlConnection cx_annuaire;
		cx_annuaire = new SqlConnection();
		cx_annuaire.ConnectionString = c_string;

		try
		{
			// ouverture
			cx_annuaire.Open();

			// opérations SQL 
			// ...
		}
		catch (SqlException err)
		{
			Trace.Write(err.Message);
		}
		finally
		{
			try
			{
				// fermeture
				cx_annuaire.Close();
			}
			catch (Exception err2)
			{
				Trace.Write(err2.Message);
			}
		}
	}
}
