Posts

Showing posts from March, 2012

ADO.NET Code Examples

http://msdn.microsoft.com/en-us/library/dw70f090.aspx

How to get data in DataSet or DataTable from DataReader using SQL Perameters

The C# Part string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;  SqlConnection mySqlConnection = new SqlConnection(connStr);         mySqlConnection.Open();         SqlCommand mySqlCommand = new SqlCommand("GetPersonByID", mySqlConnection);         //Use this if you want to use a store procedure         mySqlCommand.CommandType = CommandType.StoredProcedure;         mySqlCommand.Parameters.AddWithValue("@id", ID);         SqlDataReader dr = mySqlCommand.ExecuteReader();         dt.Load(dr);         GridView1.DataSource = dt;         GridView1.DataBind();

How to get data in DataSet or DataTable with C#

The C# Part string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; SqlConnection mySqlConnection = new SqlConnection(connStr); mySqlConnection.Open(); SqlDataAdapter da = new SqlDataAdapter("select * from Person", mySqlConnection); da.Fill(dt); The Web.Config Part (windows authentication) <connectionStrings>   <add name="myConnectionString" connectionString="server=CHILLAX-339\SQLEXPRESS;database=TestingDB;Integrated Security=SSPI;"/>  </connectionStrings>

SQLCommand Practice

These are just to understand how to use sqlcommand functions like ExecuteNonQuery(),ExecuteScaler(); etc etc The C# Part  string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; SqlConnection mySqlConnection = new SqlConnection(connStr);         mySqlConnection.Open();         SqlCommand mySqlCommand = new SqlCommand("update Person set City = 'LHR'", mySqlConnection);               //Use this if you want to use a store procedure         //mySqlCommand.CommandType = CommandType.StoredProcedure;         int result = mySqlCommand.ExecuteNonQuery(); The Web.Config Part (windows authentication) <connectionStrings> <add name="myConnectionString" connectionString="server=CHILLAX-339\SQLEXPRESS;database=TestingDB;Integrated Security=SSPI;"/> </connectionStrings>

Simple Code : how to use ExecuteNonQuery in c#

SqlConnection mySqlConnection = new  SqlConnection ( "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;" ) ;      SqlCommand mySqlCommand = mySqlConnection.CreateCommand () ;      mySqlCommand.CommandText = "INSERT INTO Employee (ID, FirstName) VALUES ("  +        "  9, 'Jason')" ;      mySqlConnection.Open () ;      int  numberOfRows = mySqlCommand.ExecuteNonQuery () ;      Console.WriteLine ( "Number of rows added = "  + numberOfRows ) ;      DisplayRow ( mySqlCommand,  "9" ) ;      mySqlCommand.CommandText =  "UPDATE Employee SET FirstName = 'New' WHERE ID = '9'" ;      numberOfRows = mySqlCommand.ExecuteNonQuery () ;      Console.WriteLine ( "Number of rows updated = "  + numberOfRows ) ;      DisplayRow ( mySqlCommand,  "9" ) ;      mySqlCommand.CommandText = "DELETE FROM Employee WHERE ID = '9'" ;      numberOfRows =

sql sever connectivity with c# code

In C#: public static class Dac {   public static DataTable ExecuteDataTable(string sqlStatement,                                       params SqlParameter[] arrParam) {     DataTable dt = new DataTable();     // Open the connection     using (SqlConnection cnn = new SqlConnection(            "Data Source=.\sqlexpress;Initial Catalog=AcmeRentals;                                     Integrated Security=True"))     {         cnn.Open();         // Define the command         using (SqlCommand cmd = new SqlCommand())         {             cmd.Connection = cnn;             cmd.CommandType = CommandType.Text;             cmd.CommandText = sqlStatement;             // Handle the parameters             if (arrParam != null)             {                 foreach (SqlParameter param in arrParam)                     cmd.Parameters.Add(param);             }             // Define the data adapter and fill the dataset             using (SqlDataAdapter da = new SqlDataAdapter(c

Simple code to connect sql server 2008 with c#

 After wandering over the internet i got the most simplest code to connect c# with SQL server and get data in datatable or dataset.     string connString = "Data Source=\SQLEXPRESS;Initial Catalog=dbname; User ID=urID; Password=urPAss";         string sql = @"select * from table";         SqlConnection conn = new SqlConnection(connString);         try         {             conn.Open();             SqlDataAdapter da = new SqlDataAdapter(sql, conn);             da.Fill(dt);         }         catch (Exception e)         {             Console.WriteLine("Error: " + e);         }         finally         {             conn.Close();         }