1

我正在尝试将参数从 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();

4

5 回答 5

0

尝试将日期时间传递为​​标准的 yyyy-MM-dd 格式。

于 2013-03-23T09:35:50.567 回答
0

听起来好像您从客户端中的 DateTimepicker 获取日期的字符串表示形式。当您从 DateTimepicker 获取值时,将其转换为真正的 CLR Date 对象client-side。如果存在映射到 SQL 类型的 CLR 类型,则不要将字符串传递到后端。在构建命令对象客户端时选择适当的参数类型。

通常,将日期保留为日期并仅在需要为人眼显示它们时将它们转换为字符串。

编辑:例如:

        SqlClient.SqlParameter  p0;

        p0.ParameterName = "@startdate";
        p0.SqlDbType = SqlDbType.DateTime;
        p0.Value =  < a datetime object, not a string>  
        p0.Direction = ParameterDirection.Input;

然后将参数添加到参数集合。

于 2013-03-23T10:34:46.220 回答
0

使用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
于 2013-03-23T10:59:28.280 回答
0
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;
于 2013-03-23T11:38:09.620 回答
0

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);

于 2013-03-23T12:05:23.243 回答