我正在开发一个C#VS 2008/SQL Server 2005 Express网站应用程序.我已经尝试了一些针对这个问题的修复,但是我的调用栈与其他问题不同.而这些修复并没有解决我的问题.我可以采取哪些步骤来解决这个问题?
这是我的错误:
System.Data.SqlClient.SqlException was caught Message="Conversion failed when converting datetime from character string." Source=".Net SqlClient Data Provider" ErrorCode=-2146232060 LineNumber=10 Number=241 Procedure="AppendDataCT" Server="\\\\.\\pipe\\772EF469-84F1-43\\tsql\\query" State=1 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(TdsParserStateObject stateObj) 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 ADONET_namespace.ADONET_methods.AppendDataCT(DataTable dt, Dictionary`2 dic) in c:\Documents and Settings\Admin\My Documents\Visual Studio 2008\WebSites\Jerry\App_Code\ADONET methods.cs:line 102
这是相关的代码.当我调试这段代码时,"dic"只循环遍历3列名,但没有查看存储在"dt"数据表中的行值.
public static string AppendDataCT(DataTable dt, Dictionarydic) { if (dic.Count != 3) throw new ArgumentOutOfRangeException("dic can only have 3 parameters"); string connString = ConfigurationManager.ConnectionStrings["AW3_string"].ConnectionString; string errorMsg; try { using (SqlConnection conn2 = new SqlConnection(connString)) { using (SqlCommand cmd = conn2.CreateCommand()) { cmd.CommandText = "dbo.AppendDataCT"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = conn2; foreach (string s in dic.Keys) { SqlParameter p = cmd.Parameters.AddWithValue(s, dic[s]); p.SqlDbType = SqlDbType.VarChar; } conn2.Open(); cmd.ExecuteNonQuery(); conn2.Close(); errorMsg = "The Person.ContactType table was successfully updated!"; } } }
这是我的SQL存储过程:
ALTER PROCEDURE [dbo].[AppendDataCT] @col1 VARCHAR(50), @col2 VARCHAR(50), @col3 VARCHAR(50) AS BEGIN SET NOCOUNT ON; DECLARE @TEMP DATETIME SET @TEMP = (SELECT CONVERT (DATETIME, @col3)) INSERT INTO Person.ContactType (Name, ModifiedDate) VALUES( @col2, @TEMP) END
输入文件有3列.前两个是varchars,但第三个也是我认为的varchar,但它表示为"3/11/2010".在此输入文件中,示例行如下所示:"Benjamin | breakfast | 3/11/2010".我正在尝试将此日期字段从字符串转换为我的SP中的日期时间.我是以错误的方式去做的吗?
DataRow:col1 | col2 | col3 11 | A2 | 1/10/1978 12 | b2 | 2/10/1978 13 | c2 | 3/10/1978 14 | d2 | 4/10/1978
我认为Belousov Pavel是正确的.在foreach中,您将每个字典项目指定为参数.这些参数中的每一个都被定义为VarChar.通过提供的信息,可以合理地假设问题出在存储过程中.
您可以发布存储过程的代码,也可以尝试通过在那里执行存储过程来重新创建SQL Management Studio中的错误.
更新中...
查看存储过程后,代码看起来正确.我能够使用以下sql代码生成您获得的错误消息.
声明@col3 varchar(50)
设置@ col3 ='| 3/11/2010'
声明@temp datetime
设置@temp =(选择转换(datetime,@ col3))
请注意,@ col3的值以管道符开头.如果删除管道字符,它可以正常工作.
我会仔细查看字典中您从中获取参数值的值.解析数据的方式可能存在问题.
更新2
以下代码未确认可行,但我想我知道您要做的是什么.我假设你传入的DataTable有这样的数据:
col1 | col2 | col3 11 | A2 | 1/10/1978 12 | b2 | 2/10/1978 13 | c2 | 3/10/1978 14 | d2 | 4/10/1978
如果是这种情况,我们不需要最初传入的字典.我还可以假设您希望对DataTable中的每一行执行一次存储过程.下面的方法类似于你在做什么,虽然它运行每行的存储过程.
我不能确定你解释的是DataTable的第一行是否包含列的名称,如果不是没有担心的话.希望这是有道理的,如果您有疑问,请留下更多评论.
public static string TestMethod(DataTable dt) { string connString = ""; string errorMsg = string.Empty; try { //loop through each row of the datatable. Not sure if the column names is a row. foreach (DataRow row in dt.Rows) { using (SqlConnection conn2 = new SqlConnection(connString)) { using (SqlCommand cmd = conn2.CreateCommand()) { cmd.CommandText = "dbo.AppendDataCT"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = conn2; cmd.Parameters.Add(new SqlParameter() { ParameterName = "@col1", SqlDbType = SqlDbType.VarChar, Value = row[0] }); cmd.Parameters.Add(new SqlParameter() { ParameterName = "@col2", SqlDbType = SqlDbType.VarChar, Value = row[1] }); cmd.Parameters.Add(new SqlParameter() { ParameterName = "@col3", SqlDbType = SqlDbType.VarChar, Value = row[2] }); conn2.Open(); cmd.ExecuteNonQuery(); conn2.Close(); } } } errorMsg = "The Person.ContactType table was successfully updated!"; } catch { } return errorMsg; }