我正在使用.Net Compact 3.5 Windows 7 CE。
我有一个大约有50个用户的应用程序,我已经设置好了,以便每次数据库事务失败时都会收到一封电子邮件,并带有查询。
每隔一段时间我就会收到一封包含堆栈跟踪的电子邮件,其开头如下:
System.ArgumentException:值不在预期范围内。
在System.Data.SqlClient.SqlParameterCollection.Validate(Int32索引,SqlParameter值)
在System.Data.SqlClient.SqlParameterCollection.AddWithoutEvents(SqlParameter value)
在System.Data.SqlClient.SqlParameterCollection.Add(SqlParameter value)
在MedWMS.Database.startSqlConnection(字符串查询,SqlParameter []参数,SqlConnection连接,SqlCommand cmd)
在MedWMS.Database。<> c__DisplayClasse.b__8()
在MedWMS.Database.retry(行动)上 在MedWMS.Database.executeNonQuery(String query,SqlParameter []参数,String connectionString)...
导致此问题的SQL查询并不总是相同。我在SQL Server Management Studio中收到没有问题的电子邮件后几秒钟运行相同的查询。
我想知道为什么会发生这种情况。这是我关于SO的第一个问题所以如果我做错了,请告诉我。我很乐意回答任何问题以提供更多细节。
这是导致此错误的代码示例:
SqlParameter[] parameters = new SqlParameter[1];
parameters[0] = new SqlParameter("@salesOrder", this.salesOrderNumber);
string query = @"
Select InvTermsOverride from SorMaster where SalesOrder = Convert(int, @salesOrder) and InvTermsOverride = '07' --07 is for COD";
DataTable dt = Database.executeSelectQuery(query, parameters, Country.getCurrent().getSysproConnectionStrReportServer());
这是实际传递的查询:
Select InvTermsOverride from SorMaster where SalesOrder = Convert(int, '000000001138325') and InvTermsOverride = '07' --07 is for COD
以下是Database
类的相关方法:
public static DataTable executeSelectQuery(String query, SqlParameter[] parameters, string connectionString)
{
DataTable dt = new DataTable();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = null;
try
{
retry(() =>
{
cmd = startSqlConnection(query, parameters, connection, cmd);
using (SqlDataReader reader = cmd.ExecuteReader())
{
dt.Load(reader);
}
});
}
catch (Exception ex)
{
onDbConnectionCatch(cmd, ex);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
return dt;
}
public static void executeNonQuery(String query, SqlParameter[] parameters, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = null;
try
{
retry(() =>
{
cmd = startSqlConnection(query, parameters, connection, cmd);
cmd.ExecuteNonQuery();
});
}
catch (Exception ex)
{
onDbConnectionCatch(cmd, ex);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
private static void retry(Action action)
{
int retryCount = 3;
int retryInterval = 1000;
Exception lastException = null;
for (int retry = 0; retry < retryCount; retry++)
{
try
{
if (retry > 0)
System.Threading.Thread.Sleep(retryInterval);
action();
lastException = null;
return;
}
catch (Exception ex)
{
lastException = ex;
}
}
if (lastException != null)
{
throw lastException;
}
}
private static SqlCommand startSqlConnection(String query, SqlParameter[] parameters, SqlConnection connection, SqlCommand cmd)
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
cmd = new SqlCommand(query, connection);
if (parameters != null)
{
foreach (SqlParameter sp in parameters)
{
if (sp != null)
{
cmd.Parameters.Add(sp);
}
}
}
return cmd;
}
private static void onDbConnectionCatch(SqlCommand cmd, Exception ex)
{
try
{
new BigButtonMessageBox("", "Unable connect to database").ShowDialog();
sendEmailWithSqlQuery(cmd, ex);
}
catch
{
}
}
private static void sendEmailWithSqlQuery(SqlCommand cmd, Exception ex)
{
string query2 = "cmd was null";
if (cmd != null)
{
query2 = cmd.CommandText;
foreach (SqlParameter p in cmd.Parameters)
{
query2 = query2.Replace(p.ParameterName, "'" + p.Value.ToString() + "'");
}
}
InternetTools.sendEmail("DB ERROR", ex.ToString() + "\r\n" + query2);
}