【问题标题】:Unable to pass parameter to the sql Date type to stored procedure from c# code无法从 c# 代码将参数传递给 sql Date 类型的存储过程
【发布时间】:2014-07-26 10:32:49
【问题描述】:

我有一个存储过程,通过比较两个日期来选择记录。从 C# 代码中,我将参数传递给存储过程,但出现错误

将参数值从字符串转换为日期时间失败。

我的 SQL Server 存储过程是:

ALTER PROCEDURE Select  
    @fromDt date = null,
    @toDt date = null,
    @flag int
AS
BEGIN   
    SET NOCOUNT ON;
    if @flag = 1
    begin   
        SELECT xxx,xxx,xxx from xxx where Status <> 'Complete'
    end
    else
    begin
        SELECT xxx,x,xxx from xxx where Status <> 'Complete' and Date >= @fromDt and Date < @toDt
    end
END
GO

我查询表的代码是:

 protected void btnFilter_Click(object sender, EventArgs e)
 {
    DoProcess(Convert.ToString(dtFrom.SelectedDate.ToString("yyyyMMdd")), Convert.ToString(dtTo.SelectedDate.ToString("yyyyMMdd")), 0);
 }

 private void DoProcess(string fromDt,string toDt, int flag)
 {
    SqlConnection connection = GeneralMethods.GetConnection();
    SqlCommand selectCommand = new SqlCommand();
    DataTable dtVendors = new DataTable();

    try 
    {
        // set command object properties
        selectCommand.Connection = connection;
        selectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        selectCommand.CommandText = "Select";
        // check if from date and to dates are not null then set parameters to stored procedure.
        if (!string.IsNullOrEmpty(fromDt))
        {                        
            selectCommand.Parameters.Add("@fromDt", SqlDbType.Date).Value = fromDt;
            selectCommand.Parameters.Add("@toDt", SqlDbType.Date).Value = toDt;
        }
        // @flag is 1 then do not compare dates if 0 then compare to and from dates.
        selectCommand.Parameters.Add("@flag", SqlDbType.Int).Value = flag;
        // open the connection execute the select command.
        connection.Open();
        // load data into data table.
        dtVendors.Load(selectCommand.ExecuteReader());
        connection.Close();

        if (dtVendors.Rows.Count > 0)
        {       
            grdVendors.DataSource = dtVendors;
            grdVendors.DataBind();                    
        }
    }
    catch (Exception ex)
    { }
    finally
    {
        // finally close the connection if open and release the resources
        if (connection.State == System.Data.ConnectionState.Open)
            connection.Close();
        connection.Dispose();
        selectCommand.Dispose();
        dtVendors.Dispose();
    }
}

我将如何解决这个错误?

【问题讨论】:

  • 把那些方法参数改成DateTime!使用 private void DoProcess(DateTime fromDt, DateTime toDt, int flag) 而不是使用字符串作为日期!
  • 在我的 sql 表中,日期的数据类型是 Date 我也应该在那里更改吗?
  • 取决于-如果您只想存储日期(没有时间)-那么Date 是完美的数据类型。如果您也需要时间 - 然后使用DateTime(SQL Server 2005 及更高版本)或DateTime2(n)(SQL Server 2008 及更高版本)
  • 如果我只在表中保留Date,那么当我将数据类型更改为DateTime 时,DataTable 会返回零行,那么它可以工作,但我不想存储时间。

标签: c# sql sql-server date stored-procedures


【解决方案1】:

为什么要将日期参数作为字符串传递?

selectCommand.Parameters.Add("@fromDt", SqlDbType.Date).Value = DateTime.Parse(fromDt);

或更好

selectCommand.Parameters.Add("@fromDt", SqlDbType.Date, DateTime.Parse(fromDt));

【讨论】:

    猜你喜欢
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2011-07-22
    相关资源
    最近更新 更多