我想从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,'')
设置@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;'
执行(@query)
而下面的代码是按键点击
的SqlConnection CON =新的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)。价值= dateTimePicker1.Value.ToString( “MM / DD / YYYY”);
cmd.Parameters.Add( “@ DT2”,SqlDbType.DateTime)。价值= dateTimePicker2.Value.ToString( “MM / DD / YYYY”);
数据表DT =新数据表();
cmd.ExecuteNonQuery();
sda.Fill(DT);
dataGridView1.DataSource = DT;
con.Close();