怎样向数据库中传递date类型的参数?

shijies 2023-05-14 17:28:06

C#有没有date类型的变量?使用Datetimepicker控件选择的是时间?

...全文
298 12 打赏 收藏 转发到动态 举报
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
shijies 05-18
  • 打赏
  • 举报
回复

我的理解是:在C#环境将日期以字符串的形式表示,在SQL语言中使用CAST()函数转成date类型?以下是建立表(包含日期类型),并插入数据的代码:

CREATE TABLE ORDER (
ORDER_DATE DATE,
CREATED_BY VARCHAR(31) DEFAULT CURRENT_USER,
ORDER_AMOUNT DECIMAL(15,2));
A new row is inserted by user JILLIBEE, omitting CREATED_BY from the
column list:
INSERT INTO ORDER (ORDER_DATE, ORDER_AMT)
VALUES ('15-SEP-2004', 1004.42);
我感觉这个日期类型与字符串没有差别

shijies 05-16
  • 打赏
  • 举报
回复

In some cases, external data stored into text files in date literal formats may be the
best solution.

shijies 05-16
  • 打赏
  • 举报
回复

从网上可以找到《Firebird数据库语言参考-数据类型转换》,可是内置函数是在SQL语言里使用的,而转换需要在C#环境进行,请赐教。

shijies 05-16
  • 打赏
  • 举报
回复

在日期类型方面firebird的参考资料:
Most host languages do not support the DATE, TIME, and TIMESTAMP types, repre-
senting them internally as strings or structures. Data capture devices usually store
dates and times in a variety of string formats and styles. Date/time types are often
incompatible between different database hosts.
Conversion generally requires evaluating and decoding the date-element content
of the source data. The second part of the process is to reconstruct the decoded ele-
ments and pass them in Firebird SQL by some means. For a host language that has no
means to pass native Firebird date/time types, use of CAST(..) in combination with
valid text strings for Firebird to process as date literals can be invaluable. 2
In some cases, external data stored into text files in date literal formats may be the
best solution. Firebird can open such files as input tables in a server-side code module—
stored procedure or trigger—and use CAST(..) and other functions to process data into
date/time columns in native tables. Refer to the section “Using External Files As Tables”
in Chapter 16 for more information.
CAST(..) can equally be used to prepare internal data for export.

shijies 05-16
  • 打赏
  • 举报
回复

好像是有ISO标准,需要根据标准转换。

shijies 05-16
  • 打赏
  • 举报
回复

firebird数据库的date类型只包含年月日信息,而 C#的 DateTime 类型包含了时间,firebird数据库有专门的函数可以在时间、日期或字符串间转换,只是这些函数是在SQL语句中使用,如果能在C#中使用函数进行转换,就可以将日期类型的变量从应用程序传递进数据库了。

  • 打赏
  • 举报
回复

在 C# 中,日期时间类型的变量是 DateTime 类型。DateTime 类型包含了日期和时间信息,可以表示从公元 0001 年 1 月 1 日到 9999 年 12 月 31 日之间的任意一个时间点。

如果需要向数据库中传递日期类型的参数,一般使用 SqlParameter 对象,并指定参数的名称、数据类型和值。例如:

DateTime date = DateTime.Now; // 当前系统时间
string sql = "INSERT INTO myTable (dateColumn) VALUES (@date)";

using (SqlConnection conn = new SqlConnection("connectionString")) {
    using (SqlCommand cmd = new SqlCommand(sql, conn)) {
        SqlParameter param = new SqlParameter("@date", SqlDbType.DateTime);
        param.Value = date;
        cmd.Parameters.Add(param);
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

在上述示例中,我们创建了一个 SqlParameter 对象,并将其添加到 SqlCommand 对象的参数集合中。参数名为 @date,类型为 SqlDbType.DateTime,值为当前系统时间 DateTime.Now。在执行 SQL 语句时,SqlCommand 会将参数值传递给数据库服务器。

至于 DatetimePicker 控件,它也是基于 DateTime 类型实现的。当用户选择日期和时间后,可以通过控件的 Value 属性获取选择的日期时间信息,然后再传递给数据库服务器。

DateTime selectedDate = dateTimePicker1.Value;
// 将 selectedDate 作为参数,执行 SQL 语句

需要注意的是,在将 DateTime 类型的值传递给 SQL Server 时,需要根据具体的数据库驱动和版本设置参数类型。例如,对于 SQL Server 2008 及以上版本,可以使用 SqlDbType.Date 表示日期类型(不含时间信息),而使用 SqlDbType.DateTime 则表示包含日期和时间信息的类型。

shijies 05-15
  • 打赏
  • 举报
回复

DateTime不是类,是结构,编程语言和数据库对它的标准是否一致?
dateTimePicker1.Value.Date

shijies 05-15
  • 打赏
  • 举报
回复

看firebird的参考资料,date类型与string类型可以使用cast()函数转化,若客户端传递string类型的参数,不进行转化与date类型就不一致。

shijies 05-15
  • 打赏
  • 举报
回复

日期类型是不是拥有日期格式的字符串?若是,就构造出相应的字符串?

shijies 05-15
  • 打赏
  • 举报
回复

数据库的字段类型是选择了date,客户端程序若使用Datetimepicker控件选择的是时间而不是日期,需要将Datetimepicker控件选择的是时间转换为日期。

  • 打赏
  • 举报
回复

数据库字段类型选择date

相关推荐

109,339

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者