Monday, December 8, 2008

Creating a transaction

SqlConnection con=new SqlConnection("User ID=" + ConfigurationSettings.AppSettings.Get("UserName").ToString() + ";Password=" + ConfigurationSettings.AppSettings.Get("Password").ToString() + ";Initial Catalog=" + ConfigurationSettings.AppSettings.Get("DBName").ToString() + ";Data Source=" + ConfigurationSettings.AppSettings.Get("ServerName").ToString() + ";Persist Security Info= true;");
con.Open();
SqlCommand cmd=new SqlCommand(sql,con);
cmd.CommandTimeout=0;
SqlTransaction t=con.BeginTransaction();
cmd.Transaction=t;
try
{
SqlDataAdapter adpt=new SqlDataAdapter(cmd);

adpt.Fill(dsTemp);
t.Commit();
con.Close();
return ds;
}
catch(Exception ex)
{
t.Rollback();
con.Close();
return new DataSet();
}

No comments: