Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- using System.Transactions; // the code is showing an error in this line
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- string connectString = "Initial Catalog=AdventureWorks;
- Data Source=SQLSERVER01;
- User id =user;password=password";
- SqlConnection con = new SqlConnection();
- con.ConnectionString = connectString;
- con.Open();
- SqlTransaction tr = null;
- tr = con.BeginTransaction();
- SqlCommand cmd = new SqlCommand("INSERT INTO sunny1(name, id, city, phone, pincode) VALUES ('sandy', 01441, 'abc', 'phone', 122001)", con, tr);
- cmd.ExecuteNonQuery();
- tr.Commit();
- Console.WriteLine("transaction completed ");
- }
- catch (SqlException e)
- {
- tr.Rollback();
- Console.WriteLine("teansaction not completed " + e.Message);
- }
- catch (System.Exception ex)
- {
- Console.WriteLine("system error " + ex.Message);
- }
- finally
- {
- cn.Close();
- }
- Console.ReadLine();
- }
- }
- SqlConnection con = new SqlConnection();
- con.ConnectionString = connectString;
- con.Open();
- SqlTransaction tr = null;
- tr = con.BeginTransaction();
- SqlCommand cmd = new SqlCommand("INSERT INTO sunny1(name, id, city, phone, pincode) VALUES ('sandy', 01441, 'abc', 'phone', 122001)", con, tr);
- cmd.ExecuteNonQuery();
- tr.Commit();
- using(SqlConnection con = new SqlConnection(connectString))
- {
- SqlTransaction tr = con.BeginTransaction();
- using(SqlCommand cmd = new SqlCommand("....", con, tr))
- {
- con.Open();
- cmd.ExecuteNonQuery();
- tr.Commit();
- con.Close();
- }
- }
- using System.Transactions;
- con.Open();
- SqlTransaction tr = null;
- tr = con.BeginTransaction();
- SqlCommand cmd = new SqlCommand("INSERT INTO sunny1(name, id, city, phone, pincode) VALUES ('sandy', 01441, 'abc', 'phone', 122001)", con, tr);
- cmd.ExecuteNonQuery();
- tr.Commit();
- con.Open();
- SqlCommand cmd = new SqlCommand("INSERT INTO sunny1(name, id, city, phone, pincode) VALUES ('sandy', 01441, 'abc', 'phone', 122001)", con);
- cmd.ExecuteNonQuery();
Add Comment
Please, Sign In to add comment