请帮忙解决这个问题 [英] Please help to solve this question

查看:56
本文介绍了请帮忙解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#Windows窗体中

在这个字符串格式的Paid_Dateis中,我将字符串数据类型转换为datetime数据类型,之后我将其插入到sql db中,显示错误。

in C# Windows Form
In this Paid_Dateis in string format, i am converting the string data type into datetime data type after i am insert it into sql db its show the error .

string P_date = row.Cells["Paid_Date"].Value.ToString();
                                DateTime h1 = new DateTime();
                               
                                h1 = DateTime.Parse(P_date );
string s=" insert into payment (paid_date) values('"+h1 +"')";
 g.ExecuteNonQuery(s, null);







在sql server中




in sql server

column name      datatype
paid_date 	    datetime	





我试过很多,但它显示如下。

sql server出错:将char数据类型转换为datetime数据类型会导致超出范围的datetime值。

语句终止...........



I tried Lot but it show like this.
Error in sql server: conversion of char data type to datetime datatype resulted in an out-of-range datetime value.
The statement as terminated...........

推荐答案

不要这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。



然后,您可以将C#验证的DateTime的实际值直接传递给数据库。

(我可以'告诉你到底该怎么做,我不知道你的g变量是什么类。



它你在那里做的,你可能有机会在其他地方连接字符串 - 所以检查并将它们更改为高优先级,否则您的数据库将被损坏或销毁!
Don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

You can then pass the actual value of the C# verified DateTime directly to the database.
(I can't tell you exactly how to do it, I have no idea what class your "g" variable is)

And it you are doing it there, the chances are you are concatenating string in other places as well - so check and change them as a high priority, or your database will be damaged or destroyed!


尝试使用此代码

日期(12/02/21 10:56:09)。ToString(MMM.dd,yyyy HH:mm:ss)

如果你使用visual studio更改日期格式< custom>

并设置自定义格式< yyyy-mm-dd>
try with this code
Date("12/02/21 10:56:09").ToString("MMM. dd, yyyy HH:mm:ss")
and if you use visual studio change date format to <custom>
and set custom format <yyyy-mm-dd>


这篇关于请帮忙解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆