简体   繁体   English

在没有参数化查询的情况下在 sql server 中插入来自 c# 的日期时间

[英]Insert date-time from c# in sql server without parametrized query's

I am working on a quite old application in which there were no parametrized query's that were used at that time.我正在开发一个相当古老的应用程序,当时没有使用参数化查询。

I have to insert date time value in an column of sql table with date-time as data type, null value is not allowed in this column.我必须在sql表的一列中插入日期时间值,数据类型为date-time ,该列中不允许使用空值。

My code.我的代码。

var expires = dtpExpires.Enabled ? dtpExpires.Value.ToString() : "'1/1/1900 12:00:00 AM'";
string query = "INSERT INTO route (expires) Values ("+ expires +")";

The problem with this is, When the date picker is disabled then a default value must be passed since null are not allowed.问题在于,当日期选择器被禁用时,必须传递一个默认值,因为 null 是不允许的。 So for that I have to include an extra '' to wrap around the date and it works correctly.因此,为此我必须包含一个额外的''来环绕日期并且它可以正常工作。

But when date picker is enabled and valid date time is trying to get inserted into database it fails due to lack of '' this wrapped around the expires variable.但是当日期选择器被启用并且有效的日期时间试图插入到数据库中时,它会由于缺少''而失败,这包裹在expires变量周围。

Is there any clean approach to do this without parametrized query.在没有参数化查询的情况下,是否有任何干净的方法可以做到这一点。 the same problem will come while updating the code.更新代码时也会出现同样的问题。 Can there be clean approach for this to work on both the cases rather than adding just if-else clause .是否可以有一种干净的方法来处理这两种情况,而不是只添加if-else子句。

EDIT编辑

To avoid "Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.DBNull'"避免“无法确定条件表达式的类型,因为‘string’和‘System.DBNull’之间没有隐式转换”

SqlCommand command = new SqlCommand("INSERT INTO route (expires) 
                                   Values (@dtpExpires)", connections);
SqlParameter dtpExpires= new SqlParameter("@dtpExpires", SqlDbType.DateTime, 10);
dtpExpires.Value = dtpExpires.Enabled ? dtpExpires.Value : DBNull.Value;
command.Parameters.Add(dtpExpires);

For you info OP@ankur给你信息 OP@ankur

Benefits of use parameters instead of concatenation 使用参数代替串联的好处

  • Safety.安全。 Concatenation opens you up to SQL-injection, especially when TB stands for Textbox.连接使您可以进行 SQL 注入,尤其是当 TB 代表文本框时。 (Obligatory XKCD cartoon ) (强制性XKCD卡通)
  • Type safety.类型安全。 You solve a lot of DateTime and number formatting issues.您解决了许多日期时间和数字格式问题。
  • Speed.速度。 The query does not change all the time, the system(s) may be able to re-use a query handle.查询不会一直更改,系统可能能够重新使用查询句柄。

Note笔记

It's better you make use of pram query to avoid sql Injection attack.最好使用 pram 查询来避免 sql 注入攻击。

since you send both datetime and null data as string, let the convertion from string to datetime handle by the sql server by using CONVERT function由于您将日期时间和空数据作为字符串发送,因此使用 CONVERT 函数让 sql server 处理从字符串到日期时间的转换

  var expires = dtpExpires.Enabled ? "'" + tpExpires.Value.ToString() + "'" : "null";

  string query = "INSERT INTO route (expires) Values (CONVERT(datetime, " + expires + "))";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM