这是正确的吗? [英] Is this is correct one or not..?

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

问题描述

嗨..我想将日期值

插入数据库。使用c#Code。如果有人知道帮我提供示例代码。



我尝试过:



cmd.Parameters.AddWithValue(@ date,Convert.ToDateTime(txtdate.Text) ));

Hi.. I want to insert the date value
to the database .using c# Code.If anyone knows help me with sample code.

What I have tried:

cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(txtdate.Text));

推荐答案

首先,不要使用Convert来处理用户输入 - 用户一直输入错误的东西,转换将导致你的应用程序失败。相反,请使用 DateTime.TryParse [< a href =https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparse?view=netframework-4.7.2\"target =_ blanktitle =New Window> ^ ]返回好/坏响应,以便您可以提示用户修复它而不是尝试向数据库添加错误数据。



然后如示例代码所示,通过参数化查询传递转换后的值。
To start with, don't use Convert to process user inputs - users type the wrong thing all the time, and Convert will cause your app to fail. Instead, use DateTime.TryParse[^] which returns a "good/bad" response so you can prompt the user to fix it instead of trying to add bad data to your DB.

Then pass the converted value via a parameterised query as your sample code shows.


这篇关于这是正确的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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