简体   繁体   English

无法将类型“object”隐式转换为“System.DateTime”。 存在显式转换(您是否缺少演员表?)

[英]Cannot implicitly convert type 'object' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

I am developing my first programm and am facing some problems please help me complete it I have this code in c#:我正在开发我的第一个程序并且遇到一些问题请帮我完成它我在 c# 中有这个代码:

SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read()) 
{
client_id = dr["clientid"].ToString();
surname = dr["surname"].ToString();
othername = dr["othername"].ToString();
gender = dr["gender"].ToString();
date_ofbirth = dr["dateofbirth"];
nationality = dr["nationality"].ToString();
//age = dr["Age"];
residential_address = dr["residentialaddress"].ToString();
postal_address = dr["postaladdress"].ToString();
contact_number = dr["telephonenumber"].ToString();
marital_status = dr["maritalstatus"].ToString();
spouse_name = dr["spousename"].ToString();
email = dr["email"].ToString();
occupation = dr["occupation"].ToString();
typeof_id = dr["typeofid"].ToString();
id_number = dr["idnumber"].ToString();
id_expirydate = dr["idexpirydate"];
remarks = dr["remarks"].ToString();
picture = dr["picture"].ToString();
return true;
cmd.CommandText = null;
}

and the error message for this is............... date_ofbirth = dr["dateofbirth"];这个错误信息是...... date_ofbirth = dr["dateofbirth"];

Error 2 Cannot implicitly convert type 'object' to 'System.DateTime'.错误 2 无法将类型“object”隐式转换为“System.DateTime”。 An explicit conversion exists存在显式转换

(are you missing a cast?) (你错过了演员吗?)

C:\Users\MICKY\Documents\Visual Studio 2008\Projects\Godswill\Godswill\Personal.cs 249 28 Godswill C:\Users\MICKY\Documents\Visual Studio 2008\Projects\Godswill\Godswill\Personal.cs 249 28 Godswill

You should cast all of those, rather than blindly using ToString() :您应该投射所有这些,而不是盲目地使用ToString()

date_ofbirth = (DateTime) dr["dateofbirth"];

This will "unbox" the value as needed.这将根据需要“拆箱”该值。

Of course, an easier approach here is to use an ORM or micro-ORM (such as "dapper") - then you just run:当然,这里更简单的方法是使用 ORM 或 micro-ORM(例如“dapper”) - 然后您只需运行:

var user = connection.Query<User>("select * from Users where Id=@id",
         new {id = 123}).First(); // (this is using "dapper")

where User is a class with properties that match the table definition, ie其中User是一个 class ,其属性与表定义匹配,即

public class User {
    public string Surname {get;set;}
    ...
    public DateTime DateOfBirth {get;set;}
}

Also;还; make sure you read about using here, ie确保您阅读了有关using here的信息,即

using(SqlDataReader dr = cmd.ExecuteReader())
{
    if (dr.Read()) {...etc...}
}

this is even more important for connections etc, but it acts to ensure the resource is correctly Dispose() d even if there is an error.这对于连接等来说更为重要,但即使出现错误,它也会确保资源正确Dispose() d。 This replaces your "init as null, set to null at the end" code, and has the advantage of actually doing something;p这将替换您的“init as null,最后设置为 null”代码,并且具有实际做某事的优势;p

When you use something like this当你使用这样的东西时

myVar = dr["myColumnName"];

the value of dr["myColumnName"] is seen by the compiler as a simple object.编译器将dr["myColumnName"]的值视为简单的 object。 You should always cast it before assigning it that way:在以这种方式分配之前,您应该始终对其进行转换:

myVar = (ExpectedType)dr["myColumnName"];
date_ofbirth = DateTime.Parse(dr["dateofbirth"].ToString());

or safe parse:或安全解析:

DateTime.TryParse(dr["dateofbirth"].ToString(), out date_ofbirth);

You will have to use Convert.ToDateTime on dr["dateofbirth"] and also on dr["idexpirydate"] (Since age would be int Convert.ToInt32 for Age in case that is failing too !)您将不得不在dr["dateofbirth"]dr["idexpirydate"] ["ideexpirydate"] 上使用Convert.ToDateTime (因为 age 将是int Convert.ToInt32 for Age 以防万一也失败!)

What is returned is of type object and you will have to cast it specifically to the DataType defined, not all of them are strings so ToString() won't be the choice for all of them.返回的是object类型,您必须将其专门转换为定义的 DataType,并非所有这些都是字符串,因此ToString()不会成为所有这些的选择。

Also it would be good to check for DBNull incase you are not using nullable datatypes此外,最好检查DBNull以防您不使用可为空的数据类型

暂无
暂无

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

相关问题 无法将类型System.DateTime隐式转换为System.DateTime? 存在显式转换。 你想念演员吗? - Cannot implicitly convert type System.DateTime to System.DateTime? Explicit conversions exist. Are you missing a cast? 无法隐式转换类型'System.DateTime?'到'System.DateTime'。存在显式转换 - Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists 无法将类型“对象”隐式转换为“ System.Data.SqlClient.SqlParameter”。 存在显式转换(您是否缺少演员表?) - Cannot implicitly convert type 'object' to 'System.Data.SqlClient.SqlParameter'. An explicit conversion exists (are you missing a cast?) 无法隐式转换类型,存在显式转换(您是否缺少强制转换?) - Cannot implicitly convert type, explicit conversion exists (are you missing a cast?) 无法隐式转换类型,存在显式转换(您是否缺少强制转换?) - Cannot implicitly convert type, an explicit conversion exists (are you missing a cast?) 无法将类型&#39;...&#39;隐式转换为&#39;System.Collections.Generic.IList &lt;...&gt;&#39;。 存在显式转换(您是否错过了演员?) - Cannot implicitly convert type '…' to 'System.Collections.Generic.IList<…>'. An explicit conversion exists (are you missing a cast?) 无法将类型&#39;System.Linq.IQueryable &lt;&gt;&#39;隐式转换为&#39;Model&#39;。存在显式转换(您是否缺少类型转换?) - Cannot implicitly convert type 'System.Linq.IQueryable<>' to 'Model'.An explicit conversion exists (are you missing a cast?) 无法将类型“ double”隐式转换为“ System.Windows.Horizo​​ntalAlignment”。 存在显式转换(您是否缺少演员表?) - Cannot implicitly convert type 'double' to 'System.Windows.HorizontalAlignment'. An explicit conversion exists (are you missing a cast?) 无法将类型&#39;System.Numerics.BigInteger&#39;隐式转换为&#39;int&#39;。 存在显式转换(您是否错过了演员?) - Cannot implicitly convert type 'System.Numerics.BigInteger' to 'int'. An explicit conversion exists (are you missing a cast?) 无法将类型&#39;System.Linq.IQueryable &lt;&gt;隐式转换为。 存在显式转换(您是否缺少演员表?) - Cannot implicitly convert type 'System.Linq.IQueryable<>to . An explicit conversion exists (are you missing a cast?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM