C# 是否将参数值从字符串转换为日期时间?

C# 是否将参数值从字符串转换为日期时间?,c#,asp.net,sql,datetime,ado,C#,Asp.net,Sql,Datetime,Ado,我在页面中有日历,我必须获得选定的日期并将其插入数据库表中 错误消息是: Insert : Failed to convert parameter value from a String to a DateTime. Insert Into Bills (ClientID,Credit,PhysicianRef,Billdate,BillImage,Status) Values (@bClientID,@bCredit,@bPhysicianRef,@bBilldate,@bBillImage

我在页面中有日历,我必须获得选定的日期并将其插入数据库表中

错误消息是:

 Insert : Failed to convert parameter value from a String to a DateTime.
Insert Into Bills (ClientID,Credit,PhysicianRef,Billdate,BillImage,Status) Values (@bClientID,@bCredit,@bPhysicianRef,@bBilldate,@bBillImage,@bStatus)Insert : Failed to convert parameter value from a String to a DateTime.
bBilldate 

您好,您需要在sql server中检查为每个字段定义了什么数据类型的表

然后在代码中使用SqlParameter类

比如说

int ClientID = Convert.ToInt32(txtClientID.text);

 yourcommandobject.Parameters.Add(new SqlParameter(@bClientID,ClientID));
其他参数也同样如此

对于datetime值,您可以这样做 将using System.Globalization命名空间添加到命名空间列表中

using System.Globalization;

Datetime Billdate = DateTime.Parse(txtDate.Text.Trim()).ToString("mm/dd/yyyy", CultureInfo.InvariantCulture);
yourcommandobject.Parameters.Add(new SqlParameter(@bBilldate,Billdate));
如果在sql server中,您的BillDate列是nvarchar/vatchar或任何字符串类型的数据类型,那么您可以这样做

string tempdate = yourcalendertool.SelectedDate;// here you need to get the selected date
string Billdate = DateTime.Parse(tempdate).ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
 yourcommandobject.Parameters.Add(new SqlParameter(@bBilldate,Billdate));

通常由区域设置、日期格式引起:dd/mm/yyyy mm/dd/yyyyyy您不理解错误的哪一部分?@Interaoi查看答案并使用sqlparameter类。我有工具日历而不是文本框有什么问题?只需更换日历工具而不是文本框。就这样。您的工作完成选定日期将像字符串一样插入,我必须将其转换为字符串?