[英]Conversion failed when converting datetime from character string
I am trying to pass parameters from windows application to a stored procedure. 我正在尝试将参数从Windows应用程序传递到存储过程。 there are two parameters '@dt' & '@dt2' which get value from datetimepickers.
有两个参数'@dt'和'@ dt2'从datetimepickers获取值。 whenever i execute the stored procedure it comes up with the error saying "Conversion failed when converting datetime from character string."
每当我执行存储过程时,都会出现错误消息:“从字符串转换日期时间时转换失败。”
After executing the query i want to display it in a datagridview. 执行查询后,我想在datagridview中显示它。
i am unable to figure out the problem.. here is my procedure and code 我无法找出问题..这是我的程序和代码
Stored Procedure 储存程序
ALTER proc [dbo].[deepak_proc]
ALTER proc [dbo]。[deepak_proc]
( @dt datetime, @dt2 datetime )
(@dt datetime,@ dt2 datetime)
AS
如
declare @cols AS NVARCHAR(MAX)
声明@cols AS NVARCHAR(MAX)
declare @query AS NVARCHAR(MAX)
声明@query AS NVARCHAR(MAX)
select @cols =
选择@cols =
STUFF((SELECT ',' + QUOTENAME(date +'_'+Logname) from ( select K_date, convert(char(10),K_date, 101) date, LogName from kq_report_analyze cross apply ( select 'In' LogName union all select 'Out' ) l ) s group by convert(char(10), K_date, 112), date, Logname order by convert(char(10), K_date, 112) FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'')
set @query
设置@query
= 'SELECT empid, name, status, '+@cols+' FROM (SELECT empid, name, status, doortime, date + "_" + col AS col_names FROM (SELECT k_userid AS [empid], K_Name AS name, k_description1 as [status], K_WorktimeUp1 AS [IN], K_WorktimeDown1 AS OUT, CONVERT(char(10), K_Date, 101) AS date FROM dbo.kq_report_analyze WHERE (K_Date BETWEEN '+@dt+' AND '+@dt2+') GROUP BY K_UserID, K_Name, k_description1, K_Date, K_WorktimeUp1, K_WorktimeDown1) src UNPIVOT (doortime FOR col IN ([IN], [OUT])) unpiv) p PIVOT (max(doortime) FOR col_names IN ('+@cols+')) piv;'
execute(@query)
执行(@query)
And the following code is on button click 并在按钮上单击以下代码
SqlConnection con = new SqlConnection();
SqlConnection con =新的SqlConnection(); con.ConnectionString = @"Data Source=.\\SQLEXPRESS;Initial Catalog=test_db;User ID=user;Password=password";
con.ConnectionString = @“ Data Source =。\\ SQLEXPRESS; Initial Catalog = test_db; User ID = user; Password = password”;
con.Open(); SqlCommand cmd = new SqlCommand("deepak_proc", con); cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dt", SqlDbType.DateTime).Value = dateTimePicker1.Value.ToString("MM/dd/yyyy");
cmd.Parameters.Add(“ @ dt”,SqlDbType.DateTime).Value = dateTimePicker1.Value.ToString(“ MM / dd / yyyy”);
cmd.Parameters.Add("@dt2", SqlDbType.DateTime).Value = dateTimePicker2.Value.ToString("MM/dd/yyyy");
cmd.Parameters.Add(“ @ dt2”,SqlDbType.DateTime).Value = dateTimePicker2.Value.ToString(“ MM / dd / yyyy”);
DataTable dt = new DataTable();
DataTable dt = new DataTable();
cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery();
sda.Fill(dt);
sda.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.DataSource = dt;
con.Close();
con.Close();
尝试将日期时间传递为标准的yyyy-MM-dd格式。
Sounds as though you're getting a string representation of a date from the DateTimepicker in the client. 听起来好像您是从客户端的DateTimepicker获取日期的字符串表示形式。 When you get the value from the DateTimepicker, convert it to a bona fide CLR Date object client-side .
从DateTimepicker获取值时,将其转换为真正的CLR Date对象client-side 。 Don't pass strings to the back-end if there is a CLR type that maps to the SQL type.
如果有CLR类型映射到SQL类型,请不要将字符串传递给后端。 Choose the appropriate parameter type when building the command object client-side.
在客户端构建命令对象时,请选择适当的参数类型。
In general, keep dates as dates and convert them to strings only when you need to display them for human eyes. 通常,将日期保留为日期,并仅在需要显示给人眼时才将它们转换为字符串。
Edit: For example: 编辑:例如:
SqlClient.SqlParameter p0;
p0.ParameterName = "@startdate";
p0.SqlDbType = SqlDbType.DateTime;
p0.Value = < a datetime object, not a string>
p0.Direction = ParameterDirection.Input;
Then add the parameter to the Parameters collection. 然后将参数添加到Parameters集合中。
Use sp_executesql system stored procedure and put your variables as parametrs 使用sp_executesql系统存储过程,并将变量作为参数
SET @query
= 'SELECT empid, name, status, ' + @cols + '
FROM (SELECT empid, name, status, doortime, date + ''_'' + col AS col_names
FROM (SELECT k_userid AS [empid], K_Name AS name,
k_description1 as [status], K_WorktimeUp1 AS [IN],
ktimeDown1 AS OUT, CONVERT(char(10), K_Date, 101) AS date
FROM dbo.kq_report_analyze
WHERE (K_Date BETWEEN @dt AND @dt2)
GROUP BY K_UserID, K_Name, k_description1, K_Date, K_WorktimeUp1,
K_WorktimeDown1) src
UNPIVOT (doortime FOR col IN ([IN], [OUT])) unpiv) p
PIVOT (max(doortime) FOR col_names IN (' + @cols + ')) piv;'
EXEC sp_executesql @query, N'@dt datetime, @dt2 datetime', @dt, @dt2
DateTime myDateTime = Convert.ToDateTime(dateTimePicker1.Value);
cmd.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter
("@myDateTime", MySql.Data.MySqlClient.MySqlDbType.DateTime));
cmd.Parameters["@myDateTime"].Value = myDateTime;
cmd.Parameters.Add("@dt", SqlDbType.DateTime).Value =DateTime.ParseExact( dateTimePicker1.Value.Tostring(),null); cmd.Parameters.Add(“ @ dt”,SqlDbType.DateTime).Value = DateTime.ParseExact(dateTimePicker1.Value.Tostring(),null);
cmd.Parameters.Add("@dt2", SqlDbType.DateTime).Value =DateTime.ParseExact(dateTimePicker2.Value.Tostring(),null); cmd.Parameters.Add(“ @ dt2”,SqlDbType.DateTime).Value = DateTime.ParseExact(dateTimePicker2.Value.Tostring(),null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.