尝试从 Access 数据库中删除时出现 C# OleDb 异常"No value given for one or more required parameters"



我有一个包含"SEMETER,SUBJECT,OFFER,RESULT"的表,其中"SEMETTER"&"主题"是主键。当我使用查询时

"DELETE FROM Course_Information WHERE Semester = 1 AND Subject = 'CSE-414' ;

它在访问数据库中运行得很好,但当我试图在c#代码中使用它时,总是会遇到异常。

此外,如果我使用"DELETE FROM Course_Information WHERE Semester=1;

我想同时使用"主题"和;WHERE条件下的"SEMETER"(因为同一学期可能有不同的科目)

查看我的代码,

connection_string = aConnection.return_connectionString(connection_string);
            string sql_query = "DELETE FROM Course_Information WHERE Semester = " + this.textBox1.Text + " AND Subject = " + this.textBox2.Text + " ;";
            OleDbConnection connect = new OleDbConnection(connection_string);
            OleDbCommand command = new OleDbCommand(sql_query, connect);
            try
            {
                connect.Open();
                OleDbDataReader reader = command.ExecuteReader();
                MessageBox.Show("Delete Successful!");
                connect.Close();
                UpdateDatabase();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

在工作示例查询中,在从this.textBox2.Text获得的值周围包含引号。

" AND Subject = '" + this.textBox2.Text + "';";

假设this.textBox2.Text包含文本foo。如果不在WHERE子句中添加这些引号,数据库引擎将看到... WHERE Semester = 1 AND Subject = foo,但在名为foo的数据源中找不到任何内容,因此假设它必须是一个参数。您需要引号来向数据库引擎发出它是字符串文字值的信号,'to'

实际上,如果切换到参数查询,就可以避免这种类型的问题,因为您不需要在DELETE语句中使用这些引号。参数查询还将保护您不受SQL注入的影响。如果恶意用户可以在this.textBox2.Text中输入或'a'='a,则表中的所有行都将被删除。

如果您在"insert"查询中遇到相同错误,您可以使用此方法来避免异常

string sqlQuery = "INSERT into EndResultOfTestCases(IDsOfCases,TestCaseName,ResultCase,ResultLog) VALUES(@ids, @casename, @results, @logs)";
        connection = new OleDbConnection(connectionStringToDB);
        command = new OleDbCommand(sqlQuery, connection);
        command.Parameters.AddWithValue("@ids",IDs);
        command.Parameters.AddWithValue("@casename", CaseName);
        command.Parameters.AddWithValue("@results", resultOfCase);
        command.Parameters.AddWithValue("@logs", logs);
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
    }

您正在从SQL查询中检查VARCHAR值。因此,您需要将"添加到查询中

校正

string sql_query = "DELETE FROM Course_Information WHERE Semester = '" + this.textBox1.Text + "' AND Subject = '" + this.textBox2.Text + "' ;";

您是否更改了任何字段名称?当我使用Access更改数据库中的字段名时,出现了此错误。旧的名字是";报销类型";。新名称是";ExpenseType";。我使用了一个带有新字段名称的查询来填充一个工作良好的数据集。

string sqlQuery = "select * from Finance WHERE ExpenseType = 'MISC'";
System.Data.OleDb.OleDbCommand obiwan = new System.Data.OleDb.OleDbCommand();
obiwan.CommandText = sqlQuery;
this.financeTableAdapter.Adapter.SelectCommand.CommandText = sqlQuery;
this.financeTableAdapter.Fill(homeFinanceDataSet.Finance);

使用相同的查询字符串打开记录集失败。

string sqlQuery = "select * from Finance WHERE ExpenseType = 'MISC'";
string dbaseaddress = "";
ADODB.Recordset RS = new ADODB.Recordset();
if (openFileDialog1.ShowDialog()== DialogResult.OK)
        {
            dbaseaddress = openFileDialog1.FileName;
            ADODB.Connection fc = new ADODB.Connection();
            string str = "Provider=Microsoft.ACE.OLEDB.12.0;" +
          "Data Source=" + dbaseaddress + ";" +
          "Jet OLEDB:Database Password=" + "password" + ";";
            fc.Open(str);
            RS.Open(sqlQuery, fc, ADODB.CursorTypeEnum.adOpenStatic, &_ 
            ADODB.LockTypeEnum.adLockOptimistic,0);

在最后一行抛出异常:";System.Runtime.InteropServices.COMException:"未为一个或多个必需参数给定值。"

当使用字段的原始名称时,不会引发异常。

string sqlQuery = "select * from Finance WHERE DisbursementType = 'MISC'";
        string dbaseaddress = "";
        ADODB.Recordset RS = new ADODB.Recordset();
        if (openFileDialog1.ShowDialog()== DialogResult.OK)
        {
            dbaseaddress = openFileDialog1.FileName;
            ADODB.Connection fc = new ADODB.Connection();
            string str = "Provider=Microsoft.ACE.OLEDB.12.0;" +
          "Data Source=" + dbaseaddress + ";" +
          "Jet OLEDB:Database Password=" + "password" + ";";
            fc.Open(str);
            RS.Open(sqlQuery, fc, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic,0);
            MessageBox.Show(string.Format("{0}",RS.RecordCount));

相关内容

最新更新