数据类型text和nvarchar在等于运算符中不兼容

时间:2020-11-29 17:03:17

this is my code

这是我的代码

ProductController.cs

public ActionResult Details(string id)
{
    product productx = productDB.products.Single(pr => pr.Product1 == id);
    return View(productx);


}

Details.aspx

    <td>
        <%-- : Html.ActionLink("Edit", "Edit", new { id=item.Id }) % --> 
        <%: Html.ActionLink("Details", "Details", new { id = item.Product1 })%>
    </td>

this is what im using to list some products from a sql database, each product have a link to a Details page to show more informations about it

这是我用来从sql数据库列出一些产品的东西,每个产品都有一个指向详细信息页面的链接,以显示有关它的更多信息

what Im trying is to only put the product label in that link to let it show something like www.mysite.com\products\battery (not the id)

我尝试的只是将产品标签放在该链接中,让它显示类似www.mysite.com \ products \ battery(不是id)

I've imagined this should work, but it throw an The data types text and nvarchar are incompatible in the equal to operator. error and neither (pr => pr.Product1.Equals(id)); works

我认为这应该工作,但它抛出一个数据类型文本和nvarchar在等于运算符不兼容。错误,两者都没有(pr => pr.Product1.Equals(id));作品

the error is clear and Im asking how should I do to make it work this way ?

错误是明确的,我问我应该怎么做让它以这种方式工作?

thanks

2 个解决方案

#1


22  

TEXT columns in SQL Server are considered Large Object data and therefore aren't indexable/searchable. They're also deprecated. So, actually, the problem is in your database, not in your application.

SQL Server中的TEXT列被视为大对象数据,因此不可索引/可搜索。他们也被弃用了。实际上,问题出在您的数据库中,而不是在您的应用程序中。

If you change the column type to a varchar(max), you can store the same amount of character data but shouldn't have this problem. Then, update your Linq to SQL entity, and you'll no longer get this particular error.

如果将列类型更改为varchar(max),则可以存储相同数量的字符数据,但不应出现此问题。然后,更新您的Linq to SQL实体,您将不再获得此特定错误。

Having said that... a column named ID shouldn't be TEXT or varchar(max), it should be an auto-increment integer ID or a GUID (uniqueidentifier), so you might want to revisit your DB design. But assuming you have good reasons for IDs to be string values of arbitrary size, the above change will allow you to filter on the column.

说过......名为ID的列不应该是TEXT或varchar(max),它应该是自动增量整数ID或GUID(uniqueidentifier),因此您可能需要重新访问数据库设计。但假设您有充分理由将ID作为任意大小的字符串值,则上述更改将允许您对列进行过滤。

#2


0  

This problem can to occur then use:

可以发生此问题然后使用:

Exemple

string sql = "Select * from TB_USER where Name = @Name";
SqlCommand cmd = new SqlCommand(sql);

This is incompatible:

这是不兼容的:

cmd.Parameters.Add("@Nome", SqlDbType.Text).Value = nome;

Change for:

cmd.Parameters.Add("@Nome", SqlDbType.VarChar, 50).Value = nome;

#1


22  

TEXT columns in SQL Server are considered Large Object data and therefore aren't indexable/searchable. They're also deprecated. So, actually, the problem is in your database, not in your application.

SQL Server中的TEXT列被视为大对象数据,因此不可索引/可搜索。他们也被弃用了。实际上,问题出在您的数据库中,而不是在您的应用程序中。

If you change the column type to a varchar(max), you can store the same amount of character data but shouldn't have this problem. Then, update your Linq to SQL entity, and you'll no longer get this particular error.

如果将列类型更改为varchar(max),则可以存储相同数量的字符数据,但不应出现此问题。然后,更新您的Linq to SQL实体,您将不再获得此特定错误。

Having said that... a column named ID shouldn't be TEXT or varchar(max), it should be an auto-increment integer ID or a GUID (uniqueidentifier), so you might want to revisit your DB design. But assuming you have good reasons for IDs to be string values of arbitrary size, the above change will allow you to filter on the column.

说过......名为ID的列不应该是TEXT或varchar(max),它应该是自动增量整数ID或GUID(uniqueidentifier),因此您可能需要重新访问数据库设计。但假设您有充分理由将ID作为任意大小的字符串值,则上述更改将允许您对列进行过滤。

#2


0  

This problem can to occur then use:

可以发生此问题然后使用:

Exemple

string sql = "Select * from TB_USER where Name = @Name";
SqlCommand cmd = new SqlCommand(sql);

This is incompatible:

这是不兼容的:

cmd.Parameters.Add("@Nome", SqlDbType.Text).Value = nome;

Change for:

cmd.Parameters.Add("@Nome", SqlDbType.VarChar, 50).Value = nome;