Reputation: 591
Update 1: The below code works fine on my DEV machine but fails on TEST/Production server.
public TransImport()
{
ConnString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
SqlConnection conn_new;
SqlCommand command_serial_new;
SqlTransaction InsertUpdateSerialNumbers;
conn_new = new SqlConnection(ConnString);
command_serial_new = conn_new.CreateCommand();
command_serial_new.CommandText = "SELECT 1 FROM YSL00020 WITH (NOLOCK) WHERE SERLNMBR = @slnr";
var p = new SqlParameter("@slnr", SqlDbType.NVarChar, 50);
command_serial_new.Parameters.Add(p);
InsertUpdateSerialNumbers = conn.BeginTransaction();
boolean bErrors = false;
while (!headerFileReader.EndOfStream)
{
headerRow = headerFileReader.ReadLine();
if (!CheckSerialNumber(headerFields[0].Trim()))
{
bErrors = true;
break;
}
}
if (bErrors)
InsertUpdateSerialNumbers.Commit();
else
InsertUpdateSerialNumbers.Rollback();
if (conn_new != null)
{
conn_new.Close();
conn_new.Dispose();
}
}
private Boolean CheckSerialNumber(string SerialNumber)
{
command_serial_new.Parameters["@slnr"].Value = SerialNumber;
try
{
var itExists = Convert.ToInt32(command_serial_new.ExecuteScalar()) > 0;
if (!itExists)
{
command_serial.Transaction = InsertUpdateSerialNumbers;
command_serial.CommandText = "INSERT INTO YSL00([Manifest_Number],[PONUMBER],[ITEMNMBR],[SERLNMBR]"
+ "VALUES ('" + Manifest + "','" + PONr + "','" + itemNumber + "','" + serialNr + "')";
var insertStatus = command_serial.ExecuteNonQuery();
return true;
}
}
catch (Exception ex)
{
LogException(ex, "Error in CheckSerialNumber =>"+ command_serial_new.CommandText.ToString());
}
return false;
}
Issues I am facing with above code:
I am using SQLTransaction with commit & rollback. I loop through a flat file and update some tables. File contains usually around 500 to 1000 lines. I am creating INSERT statement for every row (with sqltransaction) and the program halts for a long time at COMMIT statement and ultimately I get errors as shown below. However if I am comment out the entire sqltransaction (and commit plus rollback), everything is fine, but we need commit & rollback. Is there a way to create just one transaction (I am right now creating for every INSERT statement) such that I don't get errors like below.
System.ServiceModel.CommunicationException
There was an error reading from the pipe: The pipe has been ended. (109, 0x6d). mscorlib
Server stack trace: at System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) at System.ServiceModel.Channels.SessionConnectionReader.Receive(TimeSpan timeout) at System.ServiceModel.Channels.SynchronizedMessageSource.Receive(TimeSpan timeout) at System.ServiceModel.Channels.FramingDuplexSessionChannel.Receive(TimeSpan timeout) at System.ServiceModel.Channels.FramingDuplexSessionChannel.TryReceive(TimeSpan timeout, Message& message) at System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
Upvotes: 0
Views: 272
Reputation: 196
Several things that will help improve this.
First wrap SqlConnection
command in using
.
Second sounds like prod is taking longer to run this and that's why it's timing out. You can set timeout to unlimited.
Last if you are using SQL Server 2008+ you should use batch inserts.
Insert into table (column1, column2) values (value1, value2), (value3, value4)
Another side note when dynamically building these there is a limit of a thousand values being in steered at once.
Upvotes: 1