The data types text and nvarchar are incompatible in the equal to operator

后端 未结 2 1108
栀梦
栀梦 2020-12-11 03:08

this is my code

ProductController.cs

public ActionResult Details(string id)
{
    product productx = productDB.products.Single(pr =&         


        
相关标签:
2条回答
  • 2020-12-11 03:45

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-11 03:46

    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;
    
    0 讨论(0)
提交回复
热议问题