简体   繁体   English

C#在SQL CE查询(使用WebMatrix)中返回什么数据类型?

[英]What data type does C# return in a SQL CE query (using WebMatrix)?

I spent some time Googling and some time researching here: http://msdn.microsoft.com/ 我花了一些时间在Google上搜索,并在这里进行了一些研究: http : //msdn.microsoft.com/

However, I cannot seem to find a simple answer to this question: 但是,我似乎找不到这个问题的简单答案:

What C# data type is returned to the following row variable when I return some data from a database like the line shown below: 当我从数据库中返回某些数据时,将哪种C#数据类型返回到以下row变量,如下所示:

selectQueryString = "SELECT * FROM ContentObjects INNER JOIN TitleObjects ON ContentObjects.ObjectID = TitleObjects.ObjectID WHERE location='home' AND type = 'title' AND TitleObjects.ObjectID = @0";

row = db.QuerySingle(subSelectQueryString, someNumber);

I ask because I need to declare row at the beginning of the page, before I use it, but it must be initialized and I can't seem to do this without knowing the datatype. 我问是因为我需要在使用它之前在页面的开头声明row ,但是必须对其进行初始化,而且在不知道数据类型的情况下我似乎无法做到这一点。

UPDATE: 更新:

BTW, var db = Database.Open("tableName") 顺便说一句, var db = Database.Open("tableName")

The QuerySingle method returns a dynamic type (see http://msdn.microsoft.com/en-us/library/dd264736.aspx ). QuerySingle方法返回dynamic类型(请参阅http://msdn.microsoft.com/zh-cn/library/dd264736.aspx )。 The field names from the schema become properties of the dynamic object. 架构中的字段名称成为动态对象的属性。 The compiler (or in the case of dynamic , the runtime binder) will only think you are attempting to call a method if you follow the property with brackets eg 编译器(或者在dynamic的情况下,是运行时绑定器)只会认为您在尝试使用方括号括起属性的方法,例如

@row.linktext()

Unless you explicitly define your row object, it will base the columns on the SQL Server types. 除非您明确定义行对象,否则它将使列基于SQL Server类型。 You will have to know the definition of the ContectObjects table from SQL Server. 您将必须从SQL Server知道ContectObjects表的定义。

It returns a dynamic datatype 它返回一个dynamic数据类型

dynamic is a new datatype in C# 4.0. dynamic是C#4.0中的新数据类型。 Its simply a datatype that is handled on runtime. 它只是在运行时处理的数据类型。 dynamic figures out datatypes of an object after compilation eg 在编译后动态找出对象的数据类型,例如

selectQueryString = "SELECT * FROM ContentObjects INNER JOIN TitleObjects ON ContentObjects.ObjectID = TitleObjects.ObjectID WHERE location='home' AND type = 'title' AND TitleObjects.ObjectID = @0";

row = db.QuerySingle(subSelectQueryString, someNumber);

var the_field_content = row.location;

If your query method was db.Query , its going to return an IEnumerable<object> which gives you the extra advantage of digging deeper using LINQ methods or object methods eg row.Length; 如果您的查询方法是db.Query ,它将返回IEnumerable<object> ,这为您提供了使用LINQ方法或对象方法(例如row.Length;更深入挖掘的额外优势row.Length; or row.Skip(5).Take(2).Select(row); row.Skip(5).Take(2).Select(row);

Try declaring the variable row as var, hit a breakpoint on it in visual studio and hover over it. 尝试将变量行声明为var,在Visual Studio中命中一个断点,然后将鼠标悬停在该行上。 The datatype is going to show up.Its a dynamic 数据类型将显示出来。它是dynamic

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

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