数据库中的双重数据

时间:2017-02-20 02:46:10

标签: c# web-services webmethod

    [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;
        }

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

问题是数据保存两次到数据库中。例如,我登录系统,然后它返回成功,但我检查数据库有两个相同的记录与相同的数据保存。我使用断点运行,但没有任何重复,代码逐行运行。

有什么想法吗?提前谢谢

1 个答案:

答案 0 :(得分:0)

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;
    }

你执行了你的选择和插入两次,选择它并不重要,但插入它确实如此。从选择中删除.ExecuteNonQuery(),然后从插入中删除SqlDataAdapter