输入字符串的格式不正确
我在倒数第二行的 ExecuteNonQuery 处遇到了一个尴尬的错误。
错误按摩:
执行用户定义例程或聚合“sp_InsertShuttleCount”期间发生 .NET Framework 错误: System.FormatException:输入字符串的格式不正确。 系统.FormatException: 在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 在 System.Number.ParseInt32(String s,NumberStyles 样式,NumberFormatInfo 信息) 在 AtsProcedures.InsertShuttleErrorCount(SqlString 穿梭机、SqlString 站、SqlString errorstations、SqlString 嵌套、SqlString errornests、SqlDateTime ts) .
堆栈跟踪:
在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection) 在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSetbulkCopyHandler,TdsParserStateObject stateObj) 在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior、RunBehavior runBehavior、布尔 returnStream、布尔异步) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior runBehavior、布尔 returnStream、字符串方法、DbAsyncResult 结果) 在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult 结果,字符串方法名称,布尔值 sendToPipe) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 C:\Code\ATSBDE\c#\Generate Data\ATS_Test_Data_Generator\ATS_Test_Data_Generator\Form1.cs 中的 ATS_Test_Data_Generator.Form1..ctor() 处:第 113 行 在 C:\Code\ATSBDE\c#\Generate Data\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(字符串 assemblyFile,证据 assemblySecurity,字符串 [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,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;
错误巢: 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 个 Nvarchar,这是一个拼写错误。
创建过程 [dbo].[sp_InsertShuttleCount] @shuttle nvarchar, @stations nvarchar, @errorstations nvarchar, @nests nvarchar, @errornests nvarchar, @ts [日期时间] 作为调用者执行 作为 外部名称 [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
我的代码有什么问题吗?
I am get an awkward error at ExecuteNonQuery in the second last row.
Errormassage:
A .NET Framework error occurred during execution of user-defined routine or aggregate "sp_InsertShuttleCount":
System.FormatException: Input string was not in a correct format.
System.FormatException:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at AtsProcedures.InsertShuttleErrorCount(SqlString shuttle, SqlString stations, SqlString errorstations, SqlString nests, SqlString errornests, SqlDateTime ts)
.
Stacktrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ATS_Test_Data_Generator.Form1..ctor() in C:\Code\ATSBDE\c#\Generate Data\ATS_Test_Data_Generator\ATS_Test_Data_Generator\Form1.cs:line 113
at ATS_Test_Data_Generator.Program.Main() in C:\Code\ATSBDE\c#\Generate Data\ATS_Test_Data_Generator\ATS_Test_Data_Generator\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
The Values of the Strings
Station: 0;5;10;15;20;30;35;40;55;60;65;70;80;85;90;100;110;120;125;
Errorstations: 5;3;4;4;6;6;4;5;4;4;6;4;4;5;5;4;5;3;5;
Nests: 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 is a Datetime: d.ToString() "10.02.2012 06:01:00" string
The Stored Procedure takes 4 Nvarchar(4000) and one Datetime values
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();
}
Stored Procedure
It's a CLR Procedure wich works fine, since it's used frequently. And there are of course 5 Nvarchars that was a typo.
CREATE PROCEDURE [dbo].[sp_InsertShuttleCount]
@shuttle nvarchar,
@stations nvarchar,
@errorstations nvarchar,
@nests nvarchar,
@errornests nvarchar,
@ts [datetime]
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [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
What is wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题可能出在
reader.GetInt32(0)
上。您很可能遇到了无法正确解析的值。发生错误时尝试检查 reader[0] 的值。另请记住,如果 GetInt32 遇到数据库中的 NULL 值,它将失败。如果有 NULL,请首先使用适当的 if 条件调用reader.IsDBNUll(0)
。Your problem is probably with
reader.GetInt32(0)
. You're most likely hitting a value that doesn't parse properly. Try examining the value of reader[0] when the error occurs. Also bear in mind that GetInt32 will fail if it hits a NULL value in the database. If you have NULLs callreader.IsDBNUll(0)
first with an appropriate if condition.我对每个字符串末尾的分号有问题......
I i had problems with the semicolon at the end of each string...