【问题标题】:data not being updated to table数据未更新到表中
【发布时间】:2016-01-07 06:31:11
【问题描述】:

我正在尝试实现密码更改功能,但它似乎不想工作。

 private void button3_Click(object sender, EventArgs e)
    {

        using (OleDbConnection con = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\User\Desktop\esoft\gym\gym\bin\Debug\Clients.accdb"))
        {
            DataTable dt = new DataTable();
            con.Open();
            errorProvider1.Clear();
            if (dt.Rows[0][0].ToString() == "1")
            {
                if (textBox3.Text == textBox4.Text)
                {

                    OleDbDataAdapter da = new OleDbDataAdapter(" COUNT (*) FROM login WHERE username= '" + textBox1.Text + "' AND [password]='" + textBox2.Text + "' ", con);
                    OleDbCommand com = new OleDbCommand("UPDATE login SET [password] = '" + textBox3.Text + "' WHERE username = '" + textBox2.Text + "'", con);
                    com.ExecuteNonQuery();



                    MessageBox.Show("password successfully changed", "success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    errorProvider1.SetError(textBox3, "passwords dont match");
                    errorProvider1.SetError(textBox4, "passwords dont match");
                }
            }

            else
            {
                errorProvider1.SetError(textBox1, "wrong username");
                errorProvider1.SetError(textBox2, "wrong pasword");

            }

        }
    }

if (dt.Rows[0][0].ToString() == "1") 行中有一个错误,指出在该位置未找到数据,但数据表中有 5 行。

当代码在没有上述行的情况下运行时,如//if (dt.Rows[0][0].ToString() == "1")

代码运行,但表中没有数据更新。

再次更新代码,仍然收到同样的错误:

OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM login WHERE username= '" + textBox1.Text + "' AND [password]='" + textBox2.Text + "' ", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Open();
            errorProvider1.Clear();
            if (dt.Rows[0][0].ToString() == "1")

【问题讨论】:

  • 更大的问题是您试图存储密码。不要那样做!!!
  • con.read() 我认为丢失了
  • 您只创建了DataTable 的一个新实例,而没有为其分配任何数据!
  • 并且代码中没有查询sn-pt来填充DataTable

标签: c# oledb


【解决方案1】:

尝试如下填写您的DataTable -

string cmdString = "SELECT * FROM login WHERE username= '" + textBox1.Text + "' AND [password]='" + textBox2.Text + "' ";
OleDbCommand cmd = new OleDbCommand(cmdString,con);
con.Open();
var dr = cmd.ExecuteReader();

DataTable dt = new DataTable();
dt.Load(dr);
con.Close()

现在您应该在表中获取数据,前提是您的选择查询是正确的。确保在连接和命令对象上使用 using 块,以便在它们超出范围时将其处理掉。

【讨论】:

  • 这也不起作用:/当我输入错误的用户名而不是显示错误提供程序时,仍然出现相同的错误(未找到数据)。当我尝试更改现有用户名的密码时,它说已保存,但实际上没有更新任何数据。
  • 好的。我多年来一直在搞乱代码,无论我做什么,我总是得到同样的错误,没有找到数据。等等。但我已经确保我的数据表中有 5 行,并且所有其他具有相同连接字符串的表单都可以正常工作。除了另一种形式,它也是一种更新形式。那么这里发生了什么......
【解决方案2】:

你只是声明数据表,没有分配任何数据

DataTable dt = new DataTable();

这就是为什么当您尝试获取 dt.Rows[0][0].ToString() 时会出错

【讨论】:

  • @iboss - 用你得到的变化和错误细节更新你的问题。
【解决方案3】:

你可以试试这个:

OleDbDataAdapter custDA = new OleDbDataAdapter();
     DataSet custDS = new DataSet();
     DataTable custTable = new DataTable("Customers");
     custTable.Columns.Add("CustomerID", typeof(String));
     custTable.Columns.Add("CompanyName", typeof(String));
     custDS.Tables.Add(custTable);
     //Use ADO objects from ADO library (msado15.dll) imported
     //  as.NET library ADODB.dll using TlbImp.exe
     ADODB.Connection adoConn = new ADODB.Connection();
     ADODB.Recordset adoRS = new ADODB.Recordset();
     adoConn.Open("Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;", "", "", -1);
     adoRS.Open("SELECT CustomerID, CompanyName FROM Customers", adoConn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
     custDA.Fill(custTable, adoRS);
     adoRS.Close();
     adoConn.Close();

你可以关注这个reference

【讨论】:

  • 感谢您的意见,但该项目是我课程的一部分,我们不应该使用 OleDb 以外的任何东西。
【解决方案4】:

正如另一个人所说,您永远不会为数据表分配值,这就是它令人窒息的原因。您的查询本身,通过字符串连接将打开您的 SQL 注入。参数化它。最后,对于您的查询,我将查询给定用户 ID 的所有记录,但仅根据限定用户 ID 而不是密码来获取用户和密码值。这样,如果返回的行数超过 1 行,则表示用户帐户重复,应特别注意。如果它返回 NO 行,则没有这样的用户。如果它返回 ONE 行,那么您可以与输入的密码进行比较,如果匹配,则您有正确的用户 ID 可以运行。

从你的开始

using( OleDbConnection con = ...) 
{
   // create command first.. Parameterize it.  In this case "@" is parameter indicator
   // for Access.  parmUserName is the parameter name to be applied.  I explicitly added
   // "parm" in front to ensure differentiation between the parameter and actual column.
   var cmd = new OleDbCommand( 
               @"select password from login where username = @parmUserName", con);

   // Now, add the parameter of proper data type.  The name of the parameter and it's value
   cmd.Parameters.AddWithValue("parmUserName", textBox1.Text);

   // create your data adapter now based on the command above
   var da = new OleDbDataAdapter(cmd);

   // NOW, create your data table object and have data adapter query and fill with rows.
   var dt = new DataTable();
   da.Fill(dt);

   // NOW, check results.
   if (dt.Rows.Count == 0)
      MessageBox.Show("No such user account");
   else if( dt.Rows.Count > 1)
      MessageBox.Show("Duplicate user account");
   else
   {
      // valid single record. Do the passwords match?
      if (textBox3.Text.Equals(dt.Rows[0]["password"].ToString()))
      {
         MessageBox.Show("Valid login, allow to continue");

         // Now, since it appears you are trying to UPDATE the password for the user,
         // build new UPDATE command and parameterize it in a similar fashion
         var cmdUpd = new OleDbCommand(
                        @"update login set password = @parmNewPwd where username = @parmUserName", con);
         // Now, add the parameter of proper data type.  The name of the parameter and it's value
         cmd.Parameters.AddWithValue("parmNewPwd", textBox3.Text);
         cmd.Parameters.AddWithValue("parmUserName", textBox1.Text);
         if (cmd.ExecuteNonQuery() == 1)
            MessageBox.Show("Password updated");
         else
            MessageBox.Show("Failed updating password");
      }
      else
         MessageBox.Show("Invalid password");
   }
}

最后注。您还应该研究清理数据,尤其是在构建 SQL 命令之前。切勿在用户可以手动输入 SQL 注入数据的地方连接字符串,并对其进行参数化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多