且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何避免使用asp.net C#在sql server中插入重复记录

更新时间:2022-11-28 18:32:55

避免重复记录的更好方法是制作数据库中的唯一列检查

A Better way to avoid Duplicate record is TO make that Column Unique in database Check
this


您好,

您有多个选项可以避免重复将数据插入数据库。

选项1:

将用户名字段设置为主键/唯一键,因此不会插入相同的用户名数据,这又会引发异常。你处理适当的异常并且使用户很亲密。

注意:这里的异常处理块和检查数据库以及抛出SL异常更具成本效益。



选项2:

创建一个存储过程,并使用表格中的数据区分检查用户名是否可用。

公共布尔GetUserAvaliablity(用户名)

如果数据已经可用,则亲密用户。





Hi,
You have multiple options to avoid duplicate insertion of data into Database.
Option 1:
Make the Username field as Primary Key/ Unique Key, so the same username data will not be inserted, which in turn throws an exception. You handle the appropriate exception and intimate the user.
Note: Here the Exception handling block and checking Database and throwing SL exception is more cost effective.

Option 2:
Create a Stored procedure and check for username availability with the Distinct of data in table.
Public bool GetUserAvaliablity(Username)
If the data is already available then intimate user.


List<string> lstUserName = new List<string>();

           lstUserName.Add("Rajesh");
           lstUserName.Add("Anand");
           lstUserName.Add("Venky");
           lstUserName.Add("Prabhu");
           lstUserName.Add("Balu");

           var userName = from name in lstUserName
                             where name == "Balu"
                             select name;


           if(userName.ToString().Length > 0)
           {
               Console.WriteLine("Name Exists");
           }


推荐此链接

HTTP://博客.conceptinfoway.net / ASP净web的应用程序,应用软件开发,网络服务/如何对避免重复的,入门的-A-记录在数据库-时,用户点击刷新/ [ ^ ]