数据库中的双数据

Double data in database

    [WebMethod]
    public string Login(string Username, string Password)
    {
        String result;
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Nicole Wong\Desktop\Inari Tracking System\Inari Tracking System\App_Data\Database1.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT Username, Password from UserData where Username = @Username AND Password = @Password", con);
        cmd.Parameters.AddWithValue("@UserName", Username);
        cmd.Parameters.AddWithValue("@Password", Password);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        // Create an instance of DataSet.
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count> 0)
        {
            DateTime dt = DateTime.Now;
            SqlCommand cmd1 = new SqlCommand("INSERT INTO ActivityLog (CreateOn, CreateBy) VALUES (@CreateOn,@CreateBy)", con);
            cmd1.Parameters.AddWithValue("@CreateOn", dt);
            cmd1.Parameters.AddWithValue("@CreateBy", Username);
            cmd1.ExecuteNonQuery();
            SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
            // Create an instance of DataSet.
            DataSet ds1 = new DataSet();
            da1.Fill(ds);
            con.Close();

            result = "Successful";
            return result;
        }
        else
        {
            result = "Fail";
            return result;
        }

这是我将用户登录时间存储到数据库中的简单网络方法。

问题是数据存入数据库两次。比如我登录系统,然后returns成功,但是我查了下数据库有两条相同的记录保存了相同的数据。我 运行 有断点但没有任何重复,代码 运行 很好地逐行。

有什么想法吗?提前谢谢你

public string Login(string Username, string Password)
{
    String result;
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Nicole Wong\Desktop\Inari Tracking System\Inari Tracking System\App_Data\Database1.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT Username, Password from UserData where Username = @Username AND Password = @Password", con);
    cmd.Parameters.AddWithValue("@UserName", Username);
    cmd.Parameters.AddWithValue("@Password", Password);
    //This us pretty much useless on a select, SELECT is a query, not a NonQuery
    //cmd.ExecuteNonQuery(); 
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    // Create an instance of DataSet.
    DataSet ds = new DataSet();
    da.Fill(ds);
    if (ds.Tables[0].Rows.Count> 0)
    {
        DateTime dt = DateTime.Now;
        SqlCommand cmd1 = new SqlCommand("INSERT INTO ActivityLog (CreateOn, CreateBy) VALUES (@CreateOn,@CreateBy)", con);
        cmd1.Parameters.AddWithValue("@CreateOn", dt);
        cmd1.Parameters.AddWithValue("@CreateBy", Username);
        cmd1.ExecuteNonQuery();
        //Don't use the DataAdapter and try to fill a dataset from an insert, all this insert will return is @@ROWCOUNT
        //SqlDataAdapter da1 = new SqlDataAdapter(cmd1); 
        // Create an instance of DataSet.
        //DataSet ds1 = new DataSet();
        //da1.Fill(ds); 
        con.Close();

        result = "Successful";
        return result;
    }
    else
    {
        result = "Fail";
        return result;
    }

您执行了 select 和插入两次,select 没那么重要,但插入很重要。从 select 中删除 .ExecuteNonQuery() 并从插入中删除 SqlDataAdapter