daily pastebin goal
84%
SHARE
TWEET

Untitled

a guest May 16th, 2018 112 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Transactions;   // the code is showing an error in this line
  7.  
  8. class Program
  9. {
  10.   static void Main(string[] args)
  11.   {
  12.     try
  13.     {
  14.        string connectString = "Initial Catalog=AdventureWorks;
  15.                                Data Source=SQLSERVER01;
  16.                                User id =user;password=password";
  17.        SqlConnection con = new SqlConnection();
  18.        con.ConnectionString = connectString;
  19.  
  20.        con.Open();
  21.  
  22.        SqlTransaction tr = null;
  23.        tr = con.BeginTransaction();
  24.  
  25.        SqlCommand cmd = new SqlCommand("INSERT INTO sunny1(name, id, city, phone, pincode) VALUES ('sandy', 01441, 'abc', 'phone', 122001)", con, tr);
  26.        cmd.ExecuteNonQuery();
  27.        tr.Commit();
  28.  
  29.        Console.WriteLine("transaction completed ");
  30.     }
  31.     catch (SqlException e)
  32.     {
  33.        tr.Rollback();
  34.        Console.WriteLine("teansaction not completed " + e.Message);
  35.     }
  36.     catch (System.Exception ex)
  37.     {
  38.        Console.WriteLine("system error " + ex.Message);
  39.     }
  40.     finally
  41.     {
  42.        cn.Close();
  43.     }
  44.  
  45.     Console.ReadLine();
  46.  }
  47. }
  48.    
  49. SqlConnection con = new SqlConnection();
  50.    con.ConnectionString = connectString;
  51.  
  52.    con.Open();
  53.  
  54.    SqlTransaction tr = null;
  55.    tr = con.BeginTransaction();
  56.  
  57.    SqlCommand cmd = new SqlCommand("INSERT INTO sunny1(name, id, city, phone, pincode) VALUES ('sandy', 01441, 'abc', 'phone', 122001)", con, tr);
  58.    cmd.ExecuteNonQuery();
  59.    tr.Commit();
  60.    
  61. using(SqlConnection con = new SqlConnection(connectString))
  62.    {
  63.       SqlTransaction tr = con.BeginTransaction();
  64.  
  65.       using(SqlCommand cmd = new SqlCommand("....", con, tr))
  66.       {
  67.          con.Open();
  68.          cmd.ExecuteNonQuery();
  69.          tr.Commit();
  70.          con.Close();
  71.       }    
  72.    }
  73.    
  74. using System.Transactions;
  75.    
  76. con.Open();
  77.  
  78.    SqlTransaction tr = null;
  79.    tr = con.BeginTransaction();
  80.  
  81.    SqlCommand cmd = new SqlCommand("INSERT INTO sunny1(name, id, city, phone, pincode) VALUES ('sandy', 01441, 'abc', 'phone', 122001)", con, tr);
  82.    cmd.ExecuteNonQuery();
  83.    tr.Commit();
  84.    
  85. con.Open();
  86.  
  87.    SqlCommand cmd = new SqlCommand("INSERT INTO sunny1(name, id, city, phone, pincode) VALUES ('sandy', 01441, 'abc', 'phone', 122001)", con);
  88.    cmd.ExecuteNonQuery();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top