简体   繁体   English

Guid 转换为 int32

[英]Guid converted to int32

i'm trying to programmatically add a where clause to the LinqDatasource of my gridview, but i keep getting an exception saying: "Operator '=' incompatible with operand types 'Guid' and 'Int32'"我正在尝试以编程方式将 where 子句添加到我的 gridview 的 LinqDatasource,但我不断收到异常消息:“运算符 '=' 与操作数类型 'Guid' 和 'Int32' 不兼容”

if (e.CommandName == "getuser")
    {
        int index = Convert.ToInt32(e.CommandArgument);    
        GridViewRow selectedRow = GridView1.Rows[index];
        Guid id = new Guid(selectedRow.Cells[1].Text);

        LinqDataSource2.Where = "UserID = " + id;
        GridView1.DataSourceID = LinqDataSource2.ID;
        GridView1.DataBind();

    }

anyone know how, when and why my Guid is converted to an int?有谁知道我的 Guid 是如何、何时以及为什么转换为 int 的?

Fixed固定的

added a line添加了一行

LinqDataSource2.WhereParameters.Add("UserID", System.Data.DbType.Guid, id.ToString());

i'm guessing int32 is a standard type when nothing is set in whereparameters, but only the where.我猜 int32 是标准类型,当 where 参数中没有设置任何内容时,只有 where 参数。 Just guessing though, if anyone knows more please inform me.只是猜测,如果有人知道更多,请告诉我。

As always with SQL - You're better using the facilities in your data access layer to pass your parameters as their actual type (with appropriate data layer -> database conversions, as required), rather than using a string.与 SQL 一样,您最好使用数据访问层中的工具将参数作为实际类型(根据需要使用适当的数据层 -> 数据库转换)而不是使用字符串传递。

It looks like LinqDataSource has a WhereParameters collection, so you'd create your where something like:看起来 LinqDataSource 有一个WhereParameters集合,所以你可以创建你的 where 类似的东西:

LinqDataSource2.Where = "UserID = @UserID";

And then add an appropriate parameter to the WhereParameters collection.然后将适当的参数添加到 WhereParameters 集合中。 (Note, I've not written such code, just pulled in a few quick details, but it looks right) (注意,我没有写过这样的代码,只是简单介绍了一些细节,但看起来是对的)

If you'll be comparing your id as a string to your GUID, then you'd need to cast the GUID field to a string.如果您要将您的 id 作为字符串与您的 GUID 进行比较,那么您需要将 GUID 字段转换为字符串。 The easiest way to achieve this is to call the ToString() method on your GUILD field.实现此目的的最简单方法是在 GUILD 字段上调用 ToString() 方法。

Try this:尝试这个:

LinqDataSource2.Where = "UserID = '" + id.ToString("B") + "'";

This will pass the GUID in such form: {00000000-0000-0000-0000-000000000000}这将以这种形式传递 GUID: {00000000-0000-0000-0000-000000000000}
Hopefully, that's what the database is expecting.希望这就是数据库所期望的。

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

相关问题 将相关表映射到IDictionary <Guid, Tuple<String, Int32> &gt; - Mapping a related table into an IDictionary<Guid, Tuple<String, Int32>> 此上下文仅支持原始类型(“例如 Int32、String 和 Guid”) - Only primitive types ('such as Int32, String, and Guid') are supported in this context 枚举值强制转换/转换为 Int32 c# - Enum value casted / converted to Int32 c# 如何检查Double是否可以转换为Int32? - How to check if a Double can be converted into a Int32? Int32是DateTime吗? - Int32 Is DateTime? INT32?与IComparable - Int32? with IComparable 显示无法创建类型“”的常量值。 在这种情况下,仅支持基本类型(例如Int32,String和Guid) - Showing Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context 从GUID的前四个字节获取Int32的最佳方法是什么? - What is the best method of getting Int32 from first four bytes of GUID? Linq to SQL Int16在SQL命令中转换为Int32 - Linq to SQL Int16 Gets Converted as Int32 In SQL Command Linq,array.Contains()生成异常:在此上下文中仅支持基本类型(例如Int32,String和Guid&#39;) - Linq, array.Contains() generating exception: Only primitive types ('such as Int32, String, and Guid') are supported in this context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM