更新数据库并使用数据表和数据适配器

2020-02-22 c# .net sql-server webforms

以下代码有什么问题?数据库不会更新,否则将始终执行else {}。该数据库确实包含带有字段密码的1行!

    protected void Button1_Click(object sender, EventArgs e)
    {

            string connectionString = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = db1; Integrated Security = True";
            SqlConnection cnn = new SqlConnection(connectionString);
            String sql = "update table1 set password = 'hello'";
            SqlCommand command = new SqlCommand(sql, cnn);
        SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);
        cnn.Open();
        command.ExecuteNonQuery();
            cnn.Close();
            if (dt.Rows.Count > 1)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Password Reset Successful! Redirecting to Homepage!')", true);
                Response.Redirect("testlogin.aspx");
            }
            else
            {
                Label1.Text = "Invalid Details";
            }

    }

Answers

Related