通过detailsview C#插入时检查重复 [英] Check duplication while inserting through detailsview C#

查看:124
本文介绍了通过detailsview C#插入时检查重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有详细信息视图和按钮,当我点击按钮时,我想检查用户名是否已经在数据库中。



我尝试了下面的代码,我用按钮测试它,它工作,但知道我想将它分配给detailsview的插入命令。我试着将它放在DetailsView1_ItemInserting中但是在if语句的情况下它给了我带有PK重复的黄色错误页面,但是在else语句的情况下工作正常。任何帮助??



我尝试过:



 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString); 
string query =从[Login]中选择Username,其中Username ='+((TextBox)DetailsView1.FindControl(TextBox1))。Text +';
SqlCommand cmd = new SqlCommand(query,con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();

dr.Read();
if(dr.HasRows == true)
{

//此处出现错误消息

}
else
{
//这里的成功消息
}

con.Close();

解决方案

< blockquote>除了SQL注入攻击(严重的是谷歌它,你冒着破坏你的数据库的风险!)你打开了自己,用一个SELECT先查看是否存在某些东西是个坏主意插入。如果两个人试图注册完全相同的用户名,会发生什么?其中一个将成功,另一个将失败,即使你的SELECT说它可用。每个用户的SQL语句顺序如下:

 User1 User2 
SELECT(可用!)
SELECT(可用!)
INSERT (成功!)
INSERT(失败 - 重复)



更好的方法是尝试INSERT并捕获异常如果它是重复的。如果没有例外,你知道它有效。如果有例外,那就是重复。


I have a details view and a button, and when I click on the button, I want to check if the username is already in the database or not.

I tried the below code and I tested it with button and it worked, but know I want to assign it to the insert command of the detailsview. I tried to put it in DetailsView1_ItemInserting but in case of if statement it gives me the yellow error page with PK duplication, but in case of else statement is works fine. Any Help??

What I have tried:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            string query = "select Username from [Login] where Username = '" + ((TextBox)DetailsView1.FindControl("TextBox1")).Text + "'";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            dr.Read();
            if (dr.HasRows == true)
            {

                //error message here

            }
            else
            {
                //success message here
            }

            con.Close();

解决方案

Apart from the "SQL Injection Attack" (Seriously, Google it, you risk the destruction of your database!) you're opening yourself up to, it's a bad idea to use a SELECT to see if something is there first and then INSERT. What would happen if two people tried to register the exact same username? One of them is going to succeed and the other is going to fail even though your SELECT said it was available. The order of SQL statements from each user would be:

User1                 User2
SELECT (available!)
                      SELECT (available!)
INSERT (succeeds!)
                      INSERT (fails - duplicate)


A better way to do it would be to just try the INSERT and catch the exception if it's a duplicate. If there's no exception, you know it worked. If there was an exception, it was a duplicate.


这篇关于通过detailsview C#插入时检查重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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