【问题标题】:How to pass datetime2(0) in wcf uri template?如何在 wcf uri 模板中传递 datetime2(0)?
【发布时间】:2015-12-24 10:12:22
【问题描述】:

我想传递一些值还包括日期时间。我正在尝试这种方式,但我收到此错误“将数据类型 nvarchar 转换为 datetime2 时出错。”

这是我的功能;

public string InsertRezervation(int GarageId, int MemberId, string StartDate, string EndDate, bool isFinish)
        {
            SqlConnection connection = null;
            SqlCommand command = null;
            int rowsAffected = 0;

            string format = "yyyyMMdd";
            DateTime startDate = new DateTime(2014, 12, 18);
            DateTime endDate = new DateTime(2014, 12, 25);
            if (StartDate != null && EndDate != null)
            {
                startDate = DateTime.ParseExact(StartDate, format, CultureInfo.InvariantCulture);
                endDate = DateTime.ParseExact(EndDate, format, CultureInfo.InvariantCulture);
            }
            connection = new SqlConnection(conn);
            connection.Open();
            command = new SqlCommand("Parking.Rezervation", connection);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.Add("@PGarageId", System.Data.SqlDbType.NVarChar).Value = GarageId;
            command.Parameters.Add("@PMemberId", System.Data.SqlDbType.NVarChar).Value = MemberId;
            command.Parameters.Add("@PStartDate", System.Data.SqlDbType.NVarChar).Value = startDate;
            command.Parameters.Add("@PEndDate", System.Data.SqlDbType.NVarChar).Value = endDate;
            command.Parameters.Add("@PIsFinish", System.Data.SqlDbType.NVarChar).Value = isFinish;
            command.CommandText = "Parking.Rezervation";
            rowsAffected = command.ExecuteNonQuery();
            connection.Close();
            return string.Format("You entered: {0}", rowsAffected.ToString());

        }

这是界面;

[OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, 
        UriTemplate = "/InsertRezervation?GarageId={GarageId}&MemberId={MemberId}&StartDate={StartDate}&EndDate={EndDate}&isFinish={isFinish}")]
        string InsertRezervation(int GarageId, int MemberId, string StartDate, string EndDate, bool isFinish);

我该如何解决这个问题。

【问题讨论】:

  • 能否添加存储过程的代码?你传递 NVARCHAR 类型的参数,如果存储过程需要不同的类型,那么你会得到错误。
  • 只是猜测,但很可能您打算将 'startDate' 和 'endDate' 作为 datetime2 参数传递 - 但实际上您将它们作为 NVarChar 类型传递:'command.Parameters.Add("@PStartDate" , System.Data.SqlDbType.NVarChar).Value = startDate;'。我建议在那里将 NVarChar 更改为 DateTime2 ;)

标签: c# sql wcf datetime datetime2


【解决方案1】:

您正在使用 SqlDbType.NVarChar 类型传递您的 DateTime 值。改成SqlDbType.DateTime2like;

command.Parameters.Add("@PStartDate", SqlDbType.DateTime2).Value = startDate;
command.Parameters.Add("@PEndDate", SqlDbType.DateTime2).Value = endDate;

如果GarageIdMemberId 适合您的列类型,也可以将SqlDbType.IntisFinish 更改为SqlDbType.Bit。不要对所有人都使用NVarChar

还可以使用using statement 自动处理您的连接和命令,而不是手动调用Close 方法。

【讨论】:

  • @mesopotamia 很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 2020-12-06
  • 2018-12-20
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 2021-09-26
相关资源
最近更新 更多