Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据类型ntext和nvarchar在等于运算符中不兼容_C#_Asp.net_Sql_Nvarchar_Ntext - Fatal编程技术网

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

C# 数据类型ntext和nvarchar在等于运算符中不兼容,c#,asp.net,sql,nvarchar,ntext,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与数据库的主表连接,并且在上有编辑/更新/删除选项 问题是,当我试图更新te数据库中的文本时,出现了一个错误 T

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

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

CREATE TABLE [dbo].[home] (
    [Id]   INT   NOT NULL,
    [text] NTEXT NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);
我在visual studio 2012中使用FormView,FormView与数据库的主表连接,并且在上有编辑/更新/删除选项

问题是,当我试图更新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>

在查询中,在任何ntext字段周围放置一个convert,以将其转换为nvarchar(max)


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

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

Alter table home 
Alter column text nvarchar(max)
GO

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

可以尝试使用
之类的

比如说

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

如果使用DBeaver时发生这种情况,则在“保存”失败后按“取消”键时,该行将被保存。我猜这是软件中的一个bug。

drrr.Close();
//SqlConnection con=新的SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“rashmi”].ToString());
SqlCommand cmd1=新的SqlCommand(“插入到注册(全名、地址、联系人、村庄、城市、地区、州、电子邮件、密码)值(@fullname、@address、@contact、@village、@city、@district、@state、@email、@password)”);
con.Open();
cmd1.Connection=con;
cmd1.CommandType=CommandType.Text;
cmd1.Parameters.AddWithValue(“@fullname”,fullname.Text.Trim().ToString());
cmd1.Parameters.AddWithValue(“@address”,address.Text.Trim().ToString());
cmd1.Parameters.AddWithValue(“@contact”,contact.Text.Trim().ToString());
cmd1.Parameters.AddWithValue(“@village”,village.Text.Trim().ToString());
cmd1.Parameters.AddWithValue(“@city”,city.Text.Trim().ToString());
cmd1.Parameters.AddWithValue(“@district”,district.Text.Trim().ToString());
cmd1.Parameters.AddWithValue(“@state”,state.Text.Trim().ToString());
//cmd1.Parameters.AddWithValue(“@crop”,crop.Text.Trim().ToString());
cmd1.Parameters.AddWithValue(“@email”,email.Text.Trim().ToString());
cmd1.Parameters.AddWithValue(“@password”,password.Text.Trim().ToString());
int行;
尝试
{
row=cmd1.ExecuteNonQuery();
如果(行>0)
{
//lbltxt.Text=“数据成功完全输入”;
ScriptManager.RegisterStartupScript(this,this.GetType(),“警报”,“警报('注册成功”);window.location='Registration.aspx';”,true);
//响应。写入(“数据成功完全输入”);
//重定向(“login.aspx”);
}
其他的
{
lbltxt.Text=“数据成功完全输入”;
lbltxt.ForeColor=System.Drawing.Color.DarkSlateBlue;
}
//否则
//{
//响应。写入(“数据未插入”);
//}
}
捕获(例外)
{
投掷;
}
最后
{
if(con.State==ConnectionState.Open)
con.Close();
}

您如何更新数据库信息?你能把代码也显示出来吗?我在主要问题中添加了代码。你可以将数据库中的ntext类型更改为nvarchar(max)。这应该可以解决错误。它成功了!:)对不起,我是新来的,我不知道该把代码放在哪里。在桌子上,但具体在哪里?t将其放在查询中引用的nText字段周围。“[text]=@original_text”=>“convert(nvarchar(max),[text])=@original_text”虽然这段代码可以解决这个问题,但它如何以及为什么解决这个问题会真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。请正确设置代码格式。
SELECT * FROM cor.Computer WHERE description LIKE 'HP NOTEBOOK'