TableAdapter.Insert不会插入到数据库表中 [英] TableAdapter.Insert does not insert into the database table

查看:86
本文介绍了TableAdapter.Insert不会插入到数据库表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来,我一直在努力应对dataadapter查询的行为。 

我很欣赏克服这一点的提示。



我在Visual Foxpro编程方面积累了相当多的经验。

我喜欢tableadapters的概念,因为我觉得这些代码可以保持代码更具可读性所需的所有行 
$用于管理连接的b $ b保留在后台。



我在数据库'Zugriffe'中有这个表,这是项目的一部分:

CREATE TABLE [dbo]。[tabelle1](

   [Id]    INT     IDENTITY(1 ,1)NOT NULL,

   [Inhalt] NCHAR(10)NULL,

   PRIMARY KEY CLUSTERED([Id] ASC)

);

该表目前包含以下值:

ID Inhalt

2 Test0

3 Test1

4 test2

5 Test4

6 Test5



创建tableadapter'tampelle1TableAdapter'。

在tableadapter中添加插入命令'InsertTesting':

INSERT INTO tabelle1

  ;                        (Inhalt)

VALUES        (@Inhalt);    

SELECT SCOPE_IDENTITY()
$


将ExecuteMode设置为'Scalar'。

这个运行时执行的代码:

------------------------------------- -------------

//在插入命令之前为表tabelle1中的值创建数据集

ZugriffeDataSet MyDataset = new ZugriffeDataSet( ); $
//在插入命令后为表tabelle1中的值创建数据集

ZugriffeDataSet MyDatasetafterit = new ZugriffeDataSet();

/ /启动tabelle1TableAdapter的新实例

ZugriffeDataSetTableAdapters.tabelle1TableAdapter MyTableAdapter =

new ZugriffeDataSetTableAdapters.tabelle1TableAdapter();
$
//用数据集填充数据集插入命令之前的tabelle1

int nzahlFill = MyTableAdapter.Fill(MyDataset.tabelle1);
$
//使用tabelle1TableAdapter的标准插入命令上传数据

string MyInhalt =" Erster";
int nzahl = MyTableAdapter.Insert(MyInhalt);
$
//使用自编码插入命令上传数据

int new_ID = Convert.ToInt32(MyTableAdapter.InsertTesting(" Test6") ;)); $
//插入符号后填充tabelle1内容的第二个数据集为
int nzahlFill2 = MyTableAdapter.Fill(MyDatasetafterit.tabelle1);

------------------------------------------------- -----------

发生这种情况:

- nzahl设置为1 =>添加1行

- new_ID设置为8 =>在运行代码之前的最高ID为6.添加了两行:最新ID:8¥
- MyDatasetafterit包含7行,包括刚刚添加的行。
全部按预期进行。

我结束调试并查看tabelle1。令我惊讶的是,我只看到五行。

再次运行代码将得到完全相同的结果,特别是new_ID将再次为8.



为什么我看不到tabelle1中添加的行,如果我能看到MyDatasetafterit中添加的两行?
$
我是否缺少另一个命令来不断将内容上传到tabelle1?


再次感谢。


 

解决方案

互联网上有丢失的例子


https://docs.microsoft.com/en-us / dotnet / framework / data / adonet / retrieve--identity-or-autonumber-values


https://www.aspsnippets.com/Articles/Add-Insert-Identity-column-to -DataTable-在-C-和-VBNet.aspx


For days I am struggling with the behavior of dataadapter queries. 
A hint to get over this would really be appreciated.

I collected quite some experience in programming Visual Foxpro.
I like the concept of tableadapters, because I think these keep the code more readable as all the lines needed 
for the management of the connections is kept in the background.

I do have this table in the database 'Zugriffe', which is part of the project:
CREATE TABLE [dbo].[tabelle1] (
    [Id]     INT        IDENTITY (1, 1) NOT NULL,
    [Inhalt] NCHAR (10) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);
The table is currently comprising these values:
ID Inhalt
2 Test0
3 Test1
4 test2
5 Test4
6 Test5

Created the tableadapter 'tabelle1TableAdapter'.
Added the insert command 'InsertTesting' in the tableadapter:
INSERT INTO tabelle1
                         (Inhalt)
VALUES        (@Inhalt);    
SELECT SCOPE_IDENTITY()

Set the ExecuteMode to 'Scalar'.
Have this code executed in runtime:
--------------------------------------------------
//Create a dataset for the values in table tabelle1 before the insert commands
ZugriffeDataSet MyDataset = new ZugriffeDataSet();
//Create a dataset for the values in table tabelle1 after the insert commands
ZugriffeDataSet MyDatasetafterit = new ZugriffeDataSet();
//Initiate new instance of tabelle1TableAdapter
ZugriffeDataSetTableAdapters.tabelle1TableAdapter MyTableAdapter =
new ZugriffeDataSetTableAdapters.tabelle1TableAdapter();
//Fill dataset with values out of tabelle1 before insert commands
int nzahlFill = MyTableAdapter.Fill(MyDataset.tabelle1);
//Use standard insert command of tabelle1TableAdapter to upload data
string MyInhalt = "Erster";
int nzahl = MyTableAdapter.Insert(MyInhalt);
//Use self coded insert command to upload data
int new_ID = Convert.ToInt32(MyTableAdapter.InsertTesting("Test6"));
//Fill second dataset with content of tabelle1 after inserting
int nzahlFill2 = MyTableAdapter.Fill(MyDatasetafterit.tabelle1);
------------------------------------------------------------
This is happening:
- nzahl is set to 1 => 1 row is added
- new_ID is set to 8 => highest ID before running the code was 6. Two lines added: Latest ID now: 8
- MyDatasetafterit contains 7 lines including the ones just added
All as expected.
I end the debugging and look into tabelle1. Much to my surprise, I just see five lines.
Running the code again will have the exact same result, in particular, new_ID will be again 8.

Why can't I see the added lines in tabelle1, if I can see the two added lines in MyDatasetafterit?
Am I missing another command to constantly upload the content into tabelle1?

Thanks again.

 

解决方案

There are lost of examples on the internet

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-identity-or-autonumber-values

https://www.aspsnippets.com/Articles/Add-Insert-Identity-column-to-DataTable-in-C-and-VBNet.aspx


这篇关于TableAdapter.Insert不会插入到数据库表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆