我在第二行的ExecuteNonQuery中遇到了一个尴尬的错误。
Errormassage:
在执行用户定义的例程或聚合“sp_InsertShuttleCount”期间发生.NET Framework错误: System.FormatException:输入字符串的格式不正确。 System.FormatException: at System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer& number,NumberFormatInfo info,Boolean parseDecimal) 在System.Number.ParseInt32(String s,NumberStyles样式,NumberFormatInfo信息) 在AtsProcedures.InsertShuttleErrorCount(SqlString shuttle,SqlString站,SqlString errorstations,SqlString嵌套,SqlString错误,SqlDateTime ts)
堆栈跟踪:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection) 在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection) 在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj) 在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString) 在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async) 在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result) 在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult结果,String methodName,Boolean sendToPipe) 在System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在C:\ Code \ ATSBDE中的ATS_Test_Data_Generator.Form1..ctor()#c#\生成数据\ ATS_Test_Data_Generator \ ATS_Test_Data_Generator \ Form1.cs:第113行 在C:\ Code \ ATSBDE \ c#\ A生成数据\ ATS_Test_Data_Generator \ ATS_Test_Data_Generator \ Program.cs中的ATS_Test_Data_Generator.Program.Main():第18行 在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args) 在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态) 在System.Threading.ThreadHelper.ThreadStart()
字符串的值
站:0; 5; 10; 15; 20; 30; 35; 40; 55; 60; 65; 70; 80; 85; 90; 100; 110; 120; 125;
错误:5; 3; 4; 4; 6; 6; 4; 5; 4; 4; 6; 4; 4; 5; 5; 4; 5; 3; 5;
巢穴:1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
ErrorNests:0 | 1 | 1 | 0 | 1 | 1 | 1 | 0; 1 | 0 | 1 | 0 | 0 | 1 | 0 | 0; 0 | 1 | 0 | 0 | 0 | 1 | 1 | 1; 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 1 | 1 | 1 | 0 | 1 | 1 | 1 | 0 1 | 1 | 0 | 1 | 0 | 1 | 1 | 1 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 0 | 1 | 1 | 1 | 1 | 0 | 0 | 1; 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 0 | 1 | 1 | 1 | 0 | 1 | 1 | 1; 1 | 0 | 0 | 1 | 0 | 1 | 0 | 1 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1; 1 | 1 | 1 | 0 | 1 | 1 | 0 | 0 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 0 | 1 | 1 | 0 | 1 | 1 | 0 | 0 1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1;
D是日期时间:d.ToString()“10.02.2012 06:01:00”字符串
存储过程需要4个Nvarchar(4000)和一个日期时间值
for (int i = 0; i <= 16; i++) {
String Stations="";
String ErrorStations = "";
String Nests = "";
String ErrorNests = "";
command = new SqlCommand(null, connection) {
CommandText = "ats.sp_getStations",
CommandType = CommandType.StoredProcedure
};
SqlDataReader reader = command.ExecuteReader();
int k = 0;
while (reader.Read()) {
Stations += reader.GetInt32(0)+";";
Nests += "1|2|3|4|5|6|7|8;";
int m=0;
for (int j = 0; j < 8; j++) {
int l;
l=r.Next(0, 2);
nestcounts[i, k, j] += l;
m += l;
if(j!=7)
ErrorNests += nestcounts[i, k, j] + "|";
else
ErrorNests += nestcounts[i, k, j] + ";";
}
stationcounts[i, k] += m;
ErrorStations += stationcounts[i, k] + ";";
k++;
}
reader.Close();
command.Dispose();
command = new SqlCommand(null, connection) {
CommandText = "exec dbo.sp_InsertShuttleCount @shuttle, @stations, @errorstations, @nests, @errornests, @ts",
};
command.Parameters.Clear();
command.Parameters.Add("@shuttle", SqlDbType.NVarChar, 4000).Value = "1";
command.Parameters.Add("@stations", SqlDbType.NVarChar, 4000).Value = Stations;
command.Parameters.Add("@errorstations", SqlDbType.NVarChar, 4000).Value = ErrorStations;
command.Parameters.Add("@nests", SqlDbType.NVarChar, 4000).Value = Nests;
command.Parameters.Add("@errornests", SqlDbType.NVarChar, 4000).Value = ErrorNests;
command.Parameters.Add("@ts", SqlDbType.DateTime).Value = d;
command.ExecuteNonQuery();
command.Dispose();
}
存储过程
这是一个工作正常的CLR程序,因为它经常使用。当然有5个Nvarchars是一个错字。
CREATE PROCEDURE [dbo]。[sp_InsertShuttleCount] @shuttle nvarchar, @stations nvarchar, @errorstations nvarchar, @nests nvarchar, @errornests nvarchar, @ts [datetime] 执行作为调用者 如 外部名称[ats-clr]。[AtsProcedures]。[InsertShuttleErrorCount] GO
EXEC sys.sp_addextendedproperty @ name = N'SqlAssemblyFile',@ value = N'AtsProcedures.cs',@ level0type = N'SCHEMA',@ level0name = N'dbo',@ level1type = N'PROCEDURE',@ level1name = N'sp_InsertShuttleCount” GO
EXEC sys.sp_addextendedproperty @ name = N'SqlAssemblyFileLine',@ value = N'51',@ level0type = N'SCHEMA',@ level0name = N'dbo',@ level1type = N'PROCEDURE',@ level1name = N'sp_InsertShuttleCount” GO
我的代码出了什么问题?
答案 0 :(得分:0)
你的问题可能是reader.GetInt32(0)
。您最有可能达到无法正确解析的值。发生错误时,请尝试检查reader [0]的值。还要记住,如果GetInt32在数据库中遇到NULL值,它将失败。如果您有NULL,请先使用适当的if条件调用reader.IsDBNUll(0)
。
答案 1 :(得分:0)
我在每个字符串末尾都有分号问题......