简体   繁体   English

为什么我在 C# Windows 窗体应用程序中收到数据类型不匹配错误

[英]Why am I getting a Datatype Mismatch error in C# Windows Forms Application

I have created a simple application every thing is working fine except update portion insertion is working fine with same table data我创建了一个简单的应用程序,除了更新部分插入使用相同的表数据工作正常外,一切正常

My code is我的代码是

private void button2_Click(object sender, EventArgs e)
{
    string cmd = ("UPDATE submissionFee SET [stdName]='" + textBox2.Text + "', [fatherName]='" + textBox3.Text + "', [program]='" + textBox4.Text + "', [adress]='" + textBox5.Text + "',[email]='" + textBox6.Text + "', [cellNum]='" + textBox7.Text + "', [isPaid] = '" + textBox8.Text + "', [SubmissionDate] = '" + dateTimePicker1.Value.ToString("MM/dd/yyyy") + "'Where [ID]='" + textBox1.Text + "'");

    try
    {
        connection.Open();

        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = cmd;

        command.ExecuteNonQuery();

        MessageBox.Show("Account Has Been Updated");
        connection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error " + ex);
        MessageBox.Show("Please Enter Valid Data");
    }
}

Error Screenshot错误截图

在此处输入图片说明

在此处输入图片说明

Probably the connection is already open when you try to open it.当您尝试打开它时,连接可能已经打开。

Either:要么:

1) Make sure you close the connection from the last time you used it. 1) 确保从上次使用时关闭连接。

2) Or, if it is sometimes supposed to be kept open, check if the connection is already open, and don't close it if it is. 2)或者,如果它有时应该保持打开状态,请检查连接是否已经打开,如果已经打开,请不要关闭它。 Something like:类似的东西:

bool bWasOpen = (connnection.State == ConnectionState.Open);
if (!bWasOpen)
    connection.Open();
...
if (!bWasOpen)
    connection.Close();

Much Worse than the crash: Your code is volunerable to Sql-injection.比崩溃更糟糕的是:您的代码对Sql 注入是 volunerable 的

--> Use parameterized sql . --> 使用参数化的 sql

The reason for this exception in the dialog is due to the connection state is already open;对话框出现这个异常的原因是因为连接状态已经打开; and hence it cannot be opened again.因此它无法再次打开。 You must close the connection in your previous statement.您必须在之前的语句中关闭连接。 Or, check if the connection closed, and then open it.或者,检查连接是否关闭,然后打开它。

Some other tips to you is给你的其他一些提示是

  1. Do not use Textbox1, Textbox2 etc., give them proper ID like txtStudentId, txtFatherName etc.,不要使用 Textbox1、Textbox2 等,给它们适当的 ID,如 txtStudentId、txtFatherName 等,
  2. User SQL Parameters to pass the values to your database.使用 SQL 参数将值传递到您的数据库。 check the sample statements below检查下面的示例语句

    String query = "UPDATE submissionFee SET stdName=@stdName, fatherName=@fatherName where id=@id;"; SqlCommand command = new SqlCommand(query, db.Connection); command.Parameters.Add("@id",txtID.txt); command.Parameters.Add("@stdName",txtStudent.Text); command.Parameters.Add("@fatherName",txtFatherName.Text); command.ExecuteNonQuery();

Please use using statement when You query to database.查询数据库时请使用using 语句 Why?为什么? Simple... it has implemented IDisposable .很简单……它已经实现了IDisposable

PS Use parameterized query to protect against SQL Injection attacks. PS 使用参数化查询来防止 SQL 注入攻击。

string insertStatement = UPDATE submissionFee SET stdName=@stdName,fatherName=@fatherName,program=@program,adress=@adress,email=@email,cellNum=@cellNum,isPaid=@isPaid,SubmissionDate=@SubmissionDate,ID=@ID
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    using (OleDbCommand command = new OleDbCommand(insertStatement, connection))
                 command.Parameters.AddWithValue("@ID",textBox1.Text);
                command.Parameters.AddWithValue("@stdname",textbox2.Text);
                command.Parameters.AddWithValue("@fathername",textBox3.Text);
                command.Parameters.AddWithValue("@program",textBox4.Text);
                command.Parameters.AddWithValue("@adress",textBox5.Text);
                command.Parameters.AddWithValue("@email",textBox6.Text);
                command.Parameters.AddWithValue("cellNum",textBox7.Text);
                command.Parameters.AddWithValue("@isPaid",textBox8.Text);
                command.Parameters.AddWithValue("@SubmissionDate",dateTimePicker1.Value.ToString("MM/dd/yyyy"));
                connection.Open();
                var results = command.ExecuteNonReader();
            }
        }

Part of code was taken from this link. 部分代码取自此链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM