Prasanta Baidya

Prasanta Baidya

  • NA
  • 44
  • 4.8k

How to write sql queries in txt file c# transaction

Mar 27 2020 4:41 AM

I want to write sql queries in txt file c# transaction. I am able to write sql queries in txt file in normal situation.

Sql query save to txt file successfullyas per my own class
 
string strSql = "insert into dbo.repayment_collection_master( AccId, productId, mem_code, memberName)Values('1','1','3','Prasanta')
 
  1. DataAccess.ExecuteNonQuery(strSql);  
  2. public static void ExecuteNonQuery(String sql)  
  3.         {            
  4.             SqlConnection con = DBConnection.Connection;  
  5.             SqlCommand cmd = new SqlCommand();  
  6.             cmd.CommandText = sql;  
  7.             cmd.Connection = con;  
  8.             cmd.CommandTimeout = 0;  
  9.             if (con.State != ConnectionState.Open)  
  10.                 con.Open();  
  11.             int i = 0;  
  12.            i= cmd.ExecuteNonQuery();  
  13.            if (i != 0)  
  14.            {  
  15.                addQueryToDb ad = new addQueryToDb(sql);  
  16.            }  
  17.             con.Close();  
  18.   
  19.         }`  
  20.   
  21.   
  22.   
  23.      public addQueryToDb(string str)  
  24.     {       
  25.         string LogFileName = string.Empty;  
  26.         string userName = "work_";        
  27.         LogFileName = DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString();  
  28.         LogFileName = HttpContext.Current.Server.MapPath("~/userWork\\" + userName + "" + LogFileName + ".txt");       
  29.         FileStream fs = new FileStream(LogFileName, FileMode.OpenOrCreate, FileAccess.Write);  
  30.         StreamWriter m_streamWriter = new StreamWriter(fs);  
  31.         m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);  
  32.         m_streamWriter.WriteLine(str);  
  33.         m_streamWriter.WriteLine("\n");  
  34.         m_streamWriter.Flush();  
  35.         m_streamWriter.Close();  
  36.         fs.Dispose();  
  37.         m_streamWriter.Dispose();  
  38.     } 
 
 But I am not able to write query from C# transaction
 
  1. command.CommandText = @"insert into dbo.repayment_collection_master( AccId, productId, mem_code, memberName)Values('1','1','3','Prasanta')"; command.ExecuteNonQuery();    
  2.     
  3. command = new SqlCommand("SP_Loan_Repayment", connection, transaction); command.CommandType = CommandType.StoredProcedure;    
  4.     
  5. command.Parameters.Add("@branchId", SqlDbType.VarChar).Value = branchId;    
  6.                     command.Parameters.Add("@centerId", SqlDbType.VarChar).Value = centerId;    
  7.                     command.Parameters.Add("@centerName", SqlDbType.VarChar).Value = centerName;    
  8.                     command.Parameters.Add("@repayDate", SqlDbType.VarChar).Value = repayDate;    
  9.                     command.Parameters.Add("@repayPrinc", SqlDbType.Decimal).Value = repayPrinc;    
  10.                     command.Parameters.Add("@repayInt", SqlDbType.Decimal).Value = repayInt;    
  11.                     command.Parameters.Add("@transactionId", SqlDbType.VarChar).Value = transactionId;    
  12.                     command.Parameters.Add("@approvedBy", SqlDbType.VarChar).Value = approvedBy;    
  13.     
  14.                     SqlParameter VoucharReset = command.Parameters.Add("@VoucharReset", SqlDbType.Int, 2);    
  15.                     VoucharReset.Direction = ParameterDirection.Output;    
  16.                     SqlParameter ReturnIndex = command.Parameters.Add("@ReturnIndex", SqlDbType.Int, 2);    
  17.                     ReturnIndex.Direction = ParameterDirection.Output;    
  18.                     SqlParameter ReturnMsg = command.Parameters.Add("@ReturnMsg", SqlDbType.VarChar, 4000);    
  19.                     ReturnMsg.Direction = ParameterDirection.Output;    
  20.                     SqlParameter ReturntransactionId = command.Parameters.Add("@ReturntransactionId", SqlDbType.VarChar, 10);    
  21.                     ReturntransactionId.Direction = ParameterDirection.Output;    
  22.     
  23. command.ExecuteNonQuery(); transaction.Commit();    
  24.     
  25. After transaction.Commit() //I want to write above query to txt file   
 
 
 
 
 
 

Answers (3)