简体   繁体   中英

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/

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:

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.

UPDATE:

BTW, var db = Database.Open("tableName")

The QuerySingle method returns a dynamic type (see http://msdn.microsoft.com/en-us/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

@row.linktext()

Unless you explicitly define your row object, it will base the columns on the SQL Server types. You will have to know the definition of the ContectObjects table from SQL Server.

It returns a dynamic datatype

dynamic is a new datatype in 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; or 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. The datatype is going to show up.Its a dynamic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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