C#TableAdapter删除数据 [英] C# TableAdapter delete Datas

查看:445
本文介绍了C#TableAdapter删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i进入我的数据库,4个表,所有都是相互连接的。

在我的情况下我只是需要向你解释其中2个。



1.) - 用户表(PK = UserNumber)

2.) - Projekt表(PK = ProjektNumber)



如果用户存在并且Projekt已签署此用户,例如:



用户:1001有projekt号码:0001;



所以一切正常,但是,添加新用户我创建了一个新表单,我正在努力添加新表单用户。



但是如果我现在想要删除这个用户,那就不行了。它的工作我的意思是,但

它做错了。



我得到了一个referenze exeption,这是逻辑的,因为用户有Projekt数字补充说。所以我发现如果你想删除一个用户,你必须检查这个用户是否有projekt号码,如果他有,它应该首先删除projektnumbers然后删除用户。



我的程序现在可以工作,如果用户有Projekt号码它会删除用户,它只是告诉你有关参考文件。



- 我得到了这个代码成功实现了



为了把这些东西放到我的表单中,我使用了Databindings,我只是将

数据表拖放到我的表单中我自动得到一些菜单点,比如跳过前进/后退,添加新的和删除..等等。



但现在,如果用户有或没有projekt数字,它删除用户。



这里是按钮代码:





i have into my database, 4 Tables, all are connected to each other.
In my Situation i just Need to explain you 2 of them.

1.) - Users Table (PK = UserNumber)
2.) - Projekt Table (PK = ProjektNumber)

If an user exists and a Projekt is signed to this user, like this example:

user: 1001 has projekt number: 0001;

So everything works fine, BUT, to add new users i created a new form, wich is working to add new users.

But if i now want to delete this user, it aint work. It is working i mean, but
it did it wrong.

I got an referenze exeption, which was Logical, cause the user had Projekt numbers added. So i discovered that if you want to delete an user, you have to check if this user have projekt numbers, and if he has, it should delete first the projektnumbers and afterwards the user.

My Programm works now like, if an user has Projekt numbers it wount delete the user, it just tells you about the References.

- I got this code succesfully implemented

To get that stuff into my form, i used Databindings, i just drag and dropped the
datatable into my form and i got some menue Points automatically, like skip Forward/backwards, add new and delete..etc.

But now, it doesnt matter if an user has or hasnt projekt numbers, it deletes the user.

here is the button code:

private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
		{
			bool delete = true;
			
                        Data.lehrlingDataSet.NV_AUFKDataTable zeilen = 
                        AUFKAdapter.GetDataByKUNR(AktualKu);

			if (zeilen != null)
			{
			
			var zeile = zeilen.FirstOrDefault(a => a.KU_NR == AktualKu);
				if (zeile != null)
				{
					MessageBox.Show("This user has projekt NR");
					delete = false;
				}
			}
			if (delete == true)
			{
				this.Validate();
				this.nS_KUBindingSource.EndEdit();
			this.tableAdapterManager.UpdateAll(this.lehrlingDataSet);
			}
		}







谢谢



迎接niko




Thank you

greets niko

推荐答案

您好Niko,



删除这里您可以在表格中再添加一列的记录显示该记录已被删除。



为此您可以将列添加为IsDeleted 类型为bool并且默认值为=。

您要删除的记录可以将列值设置为1,否则它将为0.



这是删除具有多个表的依赖关系的记录的最佳方法。







还有另一个解决方案。您可以将表的属性设置为cascade delete = true。这将帮助您删除记录。但这根本不是推荐的。因为这将从子表和父表中删除记录。



所以请选择我的第一个大部分适应的解决方案。



谢谢
Hi Niko,

Here for deleting the records you can implement one more column to your table that shows the record is been deleted or not.

For that you can add the column as "IsDeleted" of type "bool" and having default value=0.
The record you want to delete can have the column value set to 1 otherwise it will be 0.

This is the best way to delete a record having dependencies with more then one table.

Or

There is another solution to this. You can set the property of the table as "cascade delete = true". This will help you deleting the record. But this is not at all recommended. As this will delete the record from the child tables as well as parent tables too.

So go for my 1st solution that is mostly adapted.

Thanks


这篇关于C#TableAdapter删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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