如何避免使用asp.net C#在sql server中插入重复记录 [英] How to avoid insert duplicate record in sql server using asp.net C#

查看:220
本文介绍了如何避免使用asp.net C#在sql server中插入重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

i想要查看年份和月份



hi this is my code
i want to check year and month

System.Collections.Specialized.ListDictionary listDictionary
       = new System.Collections.Specialized.ListDictionary();
            listDictionary.Add("UserName", TextBox6.Text);
            listDictionary.Add("DisplayName", TextBox7.Text);
            listDictionary.Add("Year", TextBox8.Text);
            listDictionary.Add("Month", TextBox9.Text);
            listDictionary.Add("Fee", TextBox5.Text);
            listDictionary.Add("Transactions", TextBox10.Text);

            LinqDataSource3.Insert(listDictionary);

推荐答案

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

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-记录在数据库-时,用户点击刷新/ [ ^ ]


这篇关于如何避免使用asp.net C#在sql server中插入重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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