使用 SQLAdapter.Update(Dataset) 抛出错误“选择命令中没有主键”

发布于 2024-10-21 17:10:23 字数 1611 浏览 1 评论 0原文

简而言之,C# 向我抛出“不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成”,而相关表确实有主键(Identity,因为它是 MSSQL)。

我的代码如下:

       void loaddata()
    {
        try
        {
            DS = new DataSet();
            sqladapt = new SqlDataAdapter("SELECT servicetag,name,defect,primkey FROM " + Properties.Settings.Default.DBtableLaptops + "", sqlcon);
            sqladapt.FillSchema(DS, SchemaType.Source);
            sqladapt.Fill(DS);
            commandBuilder = new SqlCommandBuilder(sqladapt);
            DT = DS.Tables[0];
            Laptops_datagridview.DataSource = DT; //I'm using a datagridview to edit the data.
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
    void savedata()
    {
        //Doesn't work yet.
        try
        {
            sqladapt.Update(DS); // <-- Throws the aforementioned error.
            sqladapt.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

数据加载等工作正常,添加新行也有效,并且它保存了它。但每当我更新或删除一行时,它都会引发错误。 Primkey 是我的主键,它是一个自动递增的整数。

我一整天都在谷歌上搜索,但无济于事,人们通常遇到的问题是首先没有定义主键,但这里不是这种情况。

任何意见都将不胜感激,我已经进行了大约 6 个小时的霰弹枪调试。

编辑: 相关表的创建语句如下:

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

In short, C# throws me "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information", while the table in question DOES have a Primary key (Identity, since it's MSSQL).

My code is as following:

       void loaddata()
    {
        try
        {
            DS = new DataSet();
            sqladapt = new SqlDataAdapter("SELECT servicetag,name,defect,primkey FROM " + Properties.Settings.Default.DBtableLaptops + "", sqlcon);
            sqladapt.FillSchema(DS, SchemaType.Source);
            sqladapt.Fill(DS);
            commandBuilder = new SqlCommandBuilder(sqladapt);
            DT = DS.Tables[0];
            Laptops_datagridview.DataSource = DT; //I'm using a datagridview to edit the data.
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
    void savedata()
    {
        //Doesn't work yet.
        try
        {
            sqladapt.Update(DS); // <-- Throws the aforementioned error.
            sqladapt.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Data loading etc works fine, Adding new rows works aswell, and it saves it. But whenever i update or delete a row, it throws the error.
Primkey is my primary key, it's an auto-incrementing integer.

I've been googling all day, but to no avail, the usual problem people have is that there is no primary key defined in the first place, but that's not the case here.

Any input would be greatly appreciated, i've been shotgun-debugging for like 6 hours now.

EDIT:
The create statements for the table in question is as following:

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两人的回忆 2024-10-28 17:10:23

你需要真正让你的primkey列成为主键:

ALTER TABLE [dbo].[laptopregistratieDB_laptops]
   ADD CONSTRAINT PK_laptopregistratieDB_laptops PRIMARY KEY (primkey)

你也可以在创建表时指定这样的约束:

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL,
    constraint PK_laptopregistratieDB_laptops PRIMARY KEY (primkey)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

或者,如果你希望SQL Server随机生成约束名称(不推荐,但有些人这样做):

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

SQL如果您使用表设计器应用主键约束,服务器还将随机命名约束(右键单击要作为主键的列旁边,选择“设置主键”)

所有这些选项也可用于指定多个-列主键,[primkey] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY 变体除外。 (在设计器场景中,您必须在右键单击之前突出显示多行)。

You need to actually make your primkey column a primary key:

ALTER TABLE [dbo].[laptopregistratieDB_laptops]
   ADD CONSTRAINT PK_laptopregistratieDB_laptops PRIMARY KEY (primkey)

You can also specify such a constraint when creating a table:

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL,
    constraint PK_laptopregistratieDB_laptops PRIMARY KEY (primkey)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

or, if you want SQL Server to randomly generate the constraint name (not recommended, but some people do do it):

CREATE TABLE [dbo].[laptopregistratieDB_laptops](
    [Servicetag] [text] NOT NULL,
    [Name] [text] NOT NULL,
    [Defect] [bit] NOT NULL,
    [primkey] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

SQL Server will also randomly name the constraint if you apply a primary key constraint using the table designer (Right click next to the column you want to be primary key, choose "Set Primary Key")

All of these options can also be used to specify multi-column primary keys, with the exception of the [primkey] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY variant. (In the designer scenario, you have to highlight multiple rows before right clicking).

权谋诡计 2024-10-28 17:10:23

表中实际上有主键吗?如果有,检查主键是否设置?检查属性DS.DataTables[0].PrimaryKey

DataSet 内的DataTable 中设置主键。您可以执行以下操作:

DS.DataTables[0].PrimaryKey  = DS.DataTables[0].DataColumns[3];

这将设置用于更新的主键。

Do you actually have a primary key in the table? If yes, check whether the primary key is set or not? Check the property DS.DataTables[0].PrimaryKey

Set the primary key in the DataTable inside the DataSet. You can do something like:

DS.DataTables[0].PrimaryKey  = DS.DataTables[0].DataColumns[3];

This will set the primary key for update.

不打扰别人 2024-10-28 17:10:23

行中的错误: " sqladapt = new SqlDataAdapter("SELECT servicetag,name,defect,primkey FROM ..., 因为在您的选择查询中不包含主键或唯一的字段。

您需要通过在表中添加主字段来修复您的选择 SQL 例如:

sqladapt = new SqlDataAdapter("SELECT ID, servicetag,name,defect,primkey FROM "

希望这有帮助!

Error in line : " sqladapt = new SqlDataAdapter("SELECT servicetag,name,defect,primkey FROM ..., Because in your select query not include a field that is primary key or unique.

You need to fix your select SQL by adding a primary field in your table. For example:

sqladapt = new SqlDataAdapter("SELECT ID, servicetag,name,defect,primkey FROM "

Hope this help!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文