检查数据是否存在

时间:2012-03-29 03:57:38

标签: c# asp.net database tsql ado.net

我有一个小问题需要我一天,直到现在它还没有解决,我试图保存数据库中不存在的记录,如果用户输入已经存在,它将不会保存但是事情是它以某种方式工作,但后来我注意到当我试图验证第二行并输入相同的值,如用户名或电子邮件热潮!数据被插入,因此它会导致重复。如何解决这个问题?你能帮我吗?感谢

这是我的代码。

  protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblInternetAccount.Value = lblAccountNo.Text.ToString() + txtUsername.Text.ToString();
        DataSet ds = new DataSet();
        ds = (startWebService.getAllUsers());
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow drow in ds.Tables[0].Rows)
            {
                string username = drow["UserName"].ToString();
                string acctNo = drow["AccountNumber"].ToString();

                if (username != txtUsername.Text.ToString() || acctNo != lblAccountNo.Text.ToString())
                {
                    startWebService.insertUser(lblAccountNo.Text.ToString(), txtUsername.Text.ToString(), txtPassword.Text.ToString(), txtUsername.Text.ToString(), cboQuestion.Text.ToString(), txtAnswer.Text.ToString(), lblInternetAccount.Value.ToString(), txtPassword.Text.ToString());
                    lblMessage.Text = "Record Updated!";

                }
                else
                {
                    lblMessage.Text = "<br>Unable to create record because account number/email is already registered.Please login instead.<br><br>";

                }
            }
        }
    }

网络服务:

    private DataSet GetDataSet(string strSPROC)
    {

        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = strSPROC;
        conn.Open();
        SqlDataAdapter myDataAdapter = new SqlDataAdapter();
        myDataAdapter.SelectCommand = cmd;
        DataSet dsMT = new DataSet();
        myDataAdapter.Fill(dsMT);
        return dsMT;
        conn.Close();
    }
   [WebMethod]
    public void insertUser(string accountNo, string userName, string pAssword, string eMail, string secretQuestion, string secretAnswer,string onlineActNo,string acctkey)
    {
        Insert("ELMS_CREATEMEMBER", accountNo, userName, pAssword, eMail, secretQuestion, secretAnswer, onlineActNo,acctkey);
    }

1 个答案:

答案 0 :(得分:1)

你快到了。您只需提前执行实际插入(这将导致许多插入)。

你正在做的是:

  每个现有记录的

- &gt;   如果记录与新记录不匹配 - &gt;   插入新记录

您需要做的是首先检查所有现有记录,然后,当没有记录与您的新记录匹配时,插入新记录。

修改后的代码示例:

lblInternetAccount.Value = lblAccountNo.Text.ToString() + txtUsername.Text.ToString(); 
DataSet ds = new DataSet(); 
ds = (startWebService.getAllUsers()); 

bool isDuplicated = false;
if (ds.Tables[0].Rows.Count > 0) 
{ 
    foreach (DataRow drow in ds.Tables[0].Rows) 
    { 
        string username = drow["UserName"].ToString(); 
        string acctNo = drow["AccountNumber"].ToString(); 

        if (username == txtUsername.Text.ToString() && acctNo == lblAccountNo.Text.ToString()) 
        { 
            isDuplicated = true;
        } 

    } 

    if (!isDuplicated)
    {
        startWebService.insertUser(lblAccountNo.Text.ToString(), txtUsername.Text.ToString(), txtPassword.Text.ToString(), txtUsername.Text.ToString(), cboQuestion.Text.ToString(), txtAnswer.Text.ToString(), lblInternetAccount.Value.ToString(), txtPassword.Text.ToString()); 
        lblMessage.Text = "Record Updated!"; 
    }
    else 
    { 
        lblMessage.Text = "<br>Unable to create record because account number/email is already registered.Please login instead.<br><br>"; 
    } 
}