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

时间:2014-01-22 20:35:01

标签: c# asp.net sql nvarchar ntext

我有一个问题,我不知道如何解决它。

我在数据库中有一个简单的表

CREATE TABLE [dbo].[home] (
    [Id]   INT   NOT NULL,
    [text] NTEXT NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

我在visual studio 2012中使用FormView,FormView与数据库的Home表连接,并且有编辑/更新/删除选项。

问题在于,当我尝试更新te数据库中的文本时出现错误

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

请一点帮助..

这是我用来在DB中编写的代码:

    <%@ Page Title="" Language="C#" MasterPageFile="~/admin/adminmaster.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1">
        <EditItemTemplate>
            text:
            <asp:TextBox Text='<%# Bind("text") %>' runat="server" ID="textTextBox" /><br />
            Id:
            <asp:Label Text='<%# Eval("Id") %>' runat="server" ID="IdLabel1" /><br />
            <asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateButton" CausesValidation="True" />&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="UpdateCancelButton" CausesValidation="False" />
        </EditItemTemplate>
        <InsertItemTemplate>
            text:
            <asp:TextBox Text='<%# Bind("text") %>' runat="server" ID="textTextBox" /><br />
            Id:
            <asp:TextBox Text='<%# Bind("Id") %>' runat="server" ID="IdTextBox" /><br />
            <asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" />&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
        </InsertItemTemplate>
        <ItemTemplate>
            text:
            <asp:Label Text='<%# Bind("text") %>' runat="server" ID="textLabel" /><br />
            Id:
            <asp:Label Text='<%# Eval("Id") %>' runat="server" ID="IdLabel" /><br />
            <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditButton" CausesValidation="False" />&nbsp;<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" ID="DeleteButton" CausesValidation="False" />&nbsp;<asp:LinkButton runat="server" Text="New" CommandName="New" ID="NewButton" CausesValidation="False" />
        </ItemTemplate>
    </asp:FormView>
    <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConflictDetection="CompareAllValues" ConnectionString='<%$ ConnectionStrings:ConnectionString_SQLServer %>' DeleteCommand="DELETE FROM [home] WHERE [Id] = @original_Id AND [text] = @original_text" InsertCommand="INSERT INTO [home] ([Id], [text]) VALUES (@Id, @text)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [Id], [text] FROM [home]" UpdateCommand="UPDATE [home] SET [text] = @text WHERE [Id] = @original_Id AND [text] = @original_text">
        <DeleteParameters>
            <asp:Parameter Name="original_Id" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="original_text" Type="String"></asp:Parameter>
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Id" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="text" Type="String"></asp:Parameter>
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="text" Type="String"></asp:Parameter>
            <asp:Parameter Name="original_Id" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="original_text" Type="String"></asp:Parameter>
        </UpdateParameters>
    </asp:SqlDataSource>
</asp:Content>

这就是我想要获取文本格式DB并将其写入Label:

的地方
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


            string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString_SQLServer"].ConnectionString;

            string text = "SELECT text FROM home WHERE id=1";

            using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(text, conn);
                box1_text.Text = (string)cmd.ExecuteScalar();

            }
        }


}

5 个答案:

答案 0 :(得分:4)

在您的查询中,在任何ntext字段周围进行转换,将它们转换为nvarchar(max)

例如:convert(nvarchar(max),nTextField)

答案 1 :(得分:0)

为了避免上述异常,请在sql server中执行以下查询 -

Alter table home 
Alter column text nvarchar(max)
GO

我希望我的解决方案对您有用。

答案 2 :(得分:0)

可以尝试使用LIKE

例如

SELECT * FROM cor.Computer WHERE description LIKE 'HP NOTEBOOK'

答案 3 :(得分:0)

如果使用DBeaver发生这种情况,则在失败的“保存”后按“取消”会出现该行,无论该行将被保存。我猜这是软件中的错误。

答案 4 :(得分:-1)

Highcharts.getJSON('https://demo-live-data.highcharts.com/aapl-ohlcv.json', function (data) {

        const lineData = data.map(d => d[4]);
        

    Highcharts.stockChart('container', {

        series: [{
            type: 'line',
            data: lineData
        }],
    });
});