数据库中的异常,在C#中 [英] Exception, database, in c#
本文介绍了数据库中的异常,在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我仍然遇到同样的问题.在数据库中编辑用户时出现错误.此行中的错误:
"cmd1.ExecuteNonQuery();"
例外是很长:
System.Data.Odbc.OdbcException(0x80131937):错误[07002] [Microsoft] [Pilote ODBC Microsoft Access]冠军计数不正确.
这是我的代码:
private void bt_valider_Click(object sender, System.EventArgs e) { OdbcConnection cn = new OdbcConnection("DSN=cp3"); try { cn.Open(); } catch { MessageBox.Show("Failed to connect to data source"); } finally { OdbcCommand comm; comm = new OdbcCommand("select * from utilisateur where login=?", cn); objParameter = comm.Parameters.AddWithValue("login", old_login.Text); // comm = new OdbcCommand("select * from utilisateur where login=''@L''I", cn); // comm.Parameters.Add("@LI", old_login.Text); OdbcDataReader rs; try { rs = comm.ExecuteReader(); if (rs.Read()) { if (rs.GetString(1) == old_pass.Text) { rs.Close(); OdbcParameter objParameter2 = new OdbcParameter(); OdbcCommand cmd1 = new OdbcCommand("update utilisateur set login=?,mot_de_passe=?,niveau=?, where login=?", cn); objParameter2= cmd1.Parameters.AddWithValue("login", new_login.Text); objParameter2 = cmd1.Parameters.AddWithValue("mot_de_passe", new_pass.Text); if (new_type.SelectedItem.ToString() == "") objParameter2 = cmd1.Parameters.AddWithValue("type", "u"); if (new_type.SelectedItem.ToString() == "Administrateur") objParameter2 = cmd1.Parameters.AddWithValue("type", "a"); if (new_type.SelectedItem.ToString() == "utilisateur") objParameter2 = cmd1.Parameters.AddWithValue("type", "u"); objParameter2 = cmd1.Parameters.AddWithValue("", old_login.Text); cmd1.ExecuteNonQuery(); MessageBox.Show(this, "Invalid login or password ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); old_login.Text = ""; old_pass.Text = ""; new_login.Text = ""; new_pass.Text = ""; } else MessageBox.Show(this, "Invalid login or password ", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else MessageBox.Show(this, "User does not exist ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception eo) { MessageBox.Show(this, "Error System", "", MessageBoxButtons.OK, MessageBoxIcon.Error); old_login.Text = eo.ToString(); } } cn.Close(); }
我需要帮助,这对我很重要.
我种了.谢谢您的帮助.您的数据命令.相反,我看到了niveau
.您可能要检查一下.
它没有任何改变.
总是相同的错误.
Hi,
I''m still in the same problem. I have an error when I edit a user in the database.An error in this line:
" cmd1.ExecuteNonQuery();"
the exception is long:
System.Data.Odbc.OdbcException (0x80131937): ERROR [07002] [Microsoft][Pilote ODBC Microsoft Access]Champ COUNT incorrect.
here is my code:
private void bt_valider_Click(object sender, System.EventArgs e) { OdbcConnection cn = new OdbcConnection("DSN=cp3"); try { cn.Open(); } catch { MessageBox.Show("Failed to connect to data source"); } finally { OdbcCommand comm; comm = new OdbcCommand("select * from utilisateur where login=?", cn); objParameter = comm.Parameters.AddWithValue("login", old_login.Text); // comm = new OdbcCommand("select * from utilisateur where login=''@L''I", cn); // comm.Parameters.Add("@LI", old_login.Text); OdbcDataReader rs; try { rs = comm.ExecuteReader(); if (rs.Read()) { if (rs.GetString(1) == old_pass.Text) { rs.Close(); OdbcParameter objParameter2 = new OdbcParameter(); OdbcCommand cmd1 = new OdbcCommand("update utilisateur set login=?,mot_de_passe=?,niveau=?, where login=?", cn); objParameter2= cmd1.Parameters.AddWithValue("login", new_login.Text); objParameter2 = cmd1.Parameters.AddWithValue("mot_de_passe", new_pass.Text); if (new_type.SelectedItem.ToString() == "") objParameter2 = cmd1.Parameters.AddWithValue("type", "u"); if (new_type.SelectedItem.ToString() == "Administrateur") objParameter2 = cmd1.Parameters.AddWithValue("type", "a"); if (new_type.SelectedItem.ToString() == "utilisateur") objParameter2 = cmd1.Parameters.AddWithValue("type", "u"); objParameter2 = cmd1.Parameters.AddWithValue("", old_login.Text); cmd1.ExecuteNonQuery(); MessageBox.Show(this, "Invalid login or password ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); old_login.Text = ""; old_pass.Text = ""; new_login.Text = ""; new_pass.Text = ""; } else MessageBox.Show(this, "Invalid login or password ", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else MessageBox.Show(this, "User does not exist ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception eo) { MessageBox.Show(this, "Error System", "", MessageBoxButtons.OK, MessageBoxIcon.Error); old_login.Text = eo.ToString(); } } cn.Close(); }
I need help and it is important to me.
I planted.Thanks in advance for your help.
解决方案
It might be because you added a parameter which has a name oftype
but does not exist on the query of your data command. Instead, I seeniveau
. You might want to check on that.
it did not change anything.
Always the same error.
这篇关于数据库中的异常,在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文