Translate

Tuesday, 14 November 2017

Performing a Transaction Using ADO.NET ,ado.net Transaction, SqlConnection Transaction, use of Transaction in c# for submit data into database, sql Transaction

void SubmitForm()
    {
        string IPAddress = Convert.ToString(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
        SqlConnection objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConStr"].ConnectionString.ToString());
        SqlTransaction objTran;
        if (objCon.State == ConnectionState.Closed)
            objCon.Open();
        objTran = objCon.BeginTransaction();
        try
        {
            new SqlCommand("insert into tblSampleTest(name) values ('Pabitra Microsoft Research');", objCon, objTran).ExecuteNonQuery();
            new SqlCommand("insert into tblSampleTestLog (name,EntryDate,IPAddress) values ('Pabitra Microsoft Research',getdate(),'"+IPAddress+"');", objCon, objTran).ExecuteNonQuery();
            objTran.Commit();
        }
        catch (SqlException SqlEx)
        {
            objTran.Rollback();
            throw (SqlEx);
        }
        catch (Exception ex)
        {
            throw(ex);
        }
        finally
        {
            objCon.Close();       
        }
    }

No comments:

Post a Comment