我正在尝试将参数从 Windows 应用程序传递到存储过程。有两个参数 '@dt' 和 '@dt2' 从 datetimepickers 中获取值。每当我执行存储过程时,它都会出现错误消息“从字符串转换日期时间时转换失败”。
执行查询后,我想在 datagridview 中显示它。
我无法弄清楚问题..这是我的程序和代码
存储过程
ALTER proc [dbo].[deepak_proc]
(@dt 日期时间,@dt2 日期时间)
作为
声明 @cols AS NVARCHAR(MAX)
声明@query AS NVARCHAR(MAX)
选择@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,'')
设置@查询
= '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;'
执行(@查询)
以下代码在按钮单击上
SqlConnection con = new SqlConnection(); con.ConnectionString = @"数据源=.\SQLEXPRESS;初始目录=test_db;用户ID=用户;密码=密码";
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("@dt2", SqlDbType.DateTime).Value = dateTimePicker2.Value.ToString("MM/dd/yyyy");
数据表 dt = 新数据表();
cmd.ExecuteNonQuery();
sda.填充(dt);
dataGridView1.DataSource = dt;
con.Close();