'Why does commandTimeout raise SqlError in IDbConnection.Execute?
I have an IDbConnection to a SQL server and two stored procedures I would like to start from a console app using the Execute method on the open connection. When I run the first one, there are no problems, but the second one takes more than 30 seconds to execute, so a Timeout is raised.
According to the Internet, waiting for a command to execute is changed with the commandTimeout parameter, but after adding the commandTimeout: 0, commandTimeout: 99999, or commandTimeout: null, both Executes raise a SQL error: String or binary data would be truncated.
These errors don't happen if I run the procedure in MS SQL Server Management Studio.
What should I do to wait for a longer time for the procedure to execute and why does adding a commandTimeout give and error about truncating? I tried searching for a solution, but I'm not well versed in C# yet and didn't find anything useful.
Edit:
Some of you asked for tables or procedures, which I cannot give due to privacy reasons. However they are working correctly when executed in a query in the MS SQL Server Management Studio.
Here's the code in question:
public class MlCePredictedSqlSrvRepository : IMlCePredictedSqlSrvRepository
{
private IDbConnection _dbConnection;
public MlCePredictedSqlSrvRepository(IDbConnection dbConnection)
{
_dbConnection = dbConnection;
}
public bool ExecuteStoredProcedure()
{
try
{
string sql = @"[dbo].[StoredProcedure]";
_dbConnection.Open();
var procedure = _dbConnection.Execute(sql, commandType: CommandType.StoredProcedure, commandTimeout: 0);
_dbConnection.Close();
Console.WriteLine("Stored Procedure successfully executed.");
return true;
}
catch (Exception ex)
{
Console.WriteLine("\tException in the Stored Procedure: " + ex.GetType().Name + "; " + ex.GetBaseException().Message);
_dbConnection.Close();
return false;
}
}
}
It's called directly in the Program. The only change from working (albeit with a Timeout) to not working is the last parameter in Execute
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|