使用 SQLAdapter.Update(Dataset) 抛出错误“选择命令中没有主键”
简而言之,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要真正让你的primkey列成为主键:
你也可以在创建表时指定这样的约束:
或者,如果你希望SQL Server随机生成约束名称(不推荐,但有些人这样做):
SQL如果您使用表设计器应用主键约束,服务器还将随机命名约束(右键单击要作为主键的列旁边,选择“设置主键”)
所有这些选项也可用于指定多个-列主键,
[primkey] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY
变体除外。 (在设计器场景中,您必须在右键单击之前突出显示多行)。You need to actually make your primkey column a primary key:
You can also specify such a constraint when creating a table:
or, if you want SQL Server to randomly generate the constraint name (not recommended, but some people do do it):
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).表中实际上有主键吗?如果有,检查主键是否设置?检查属性
DS.DataTables[0].PrimaryKey
在
DataSet
内的DataTable
中设置主键。您可以执行以下操作:这将设置用于更新的主键。
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 theDataSet
. You can do something like:This will set the primary key for update.
行中的错误:
" sqladapt = new SqlDataAdapter("SELECT servicetag,name,defect,primkey FROM ...
, 因为在您的选择查询中不包含主键或唯一的字段。您需要通过在表中添加主字段来修复您的选择 SQL 例如:
希望这有帮助!
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:
Hope this help!!!