使用System.Data.OleDb.OleDbParameter ExecuteNonQuery();抛出异常:' System.InvalidOperationException'在System.Data.dll中

时间:2016-09-01 18:54:01

标签: c# ms-access

我正在处理需要同时使用SQL Server连接和本地数据库的C#程序,我可以使用ExecuteReader()轻松地从两个数据库中检索数据。这意味着连接正在成功执行。

但是当我尝试使用ExecuteNonQuery()时;它只是在处理本地数据库时抛出System.InvalidOperationException,没有其他信息。连接到SQL Server时它可以正常工作

这是我遇到问题的功能:

public String myFunction(String TLV, String SPName, String BD)
    {
        if (mdbEnabled.Checked == true)
        {
            string connectionString = string.Format("Provider={0}; Data Source={1}; Jet OLEDB:Engine Type={2}",
            "Microsoft.Jet.OLEDB.4.0", BD, 5);
            using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString))
            {
                try
                {
                    conn.Open();
                    System.Data.OleDb.OleDbCommand SP = new System.Data.OleDb.OleDbCommand();
                    SP.Connection = conn;
                    SP.CommandType = System.Data.CommandType.StoredProcedure;
                    SP.CommandText = SPName;
                    System.Data.OleDb.OleDbParameter IN_ERC = new System.Data.OleDb.OleDbParameter();
                    IN_ERC.ParameterName = "@IN_ERC"; IN_ERC.DbType = System.Data.DbType.Int32; IN_ERC.Direction = System.Data.ParameterDirection.Output; IN_ERC.Value = 0;
                    System.Data.OleDb.OleDbParameter CH_ASM = new System.Data.OleDb.OleDbParameter();
                    CH_ASM.ParameterName = "@CH_ASM"; CH_ASM.DbType = System.Data.DbType.String; CH_ASM.Direction = System.Data.ParameterDirection.Output; CH_ASM.Value = 0; CH_ASM.Size = 1000;
                    System.Data.OleDb.OleDbParameter IN_ASM = new System.Data.OleDb.OleDbParameter();
                    IN_ASM.ParameterName = "@IN_ASM"; IN_ASM.DbType = System.Data.DbType.Int32; IN_ASM.Direction = System.Data.ParameterDirection.Output; IN_ASM.Value = 0;
                    System.Data.OleDb.OleDbParameter CH_DSM = new System.Data.OleDb.OleDbParameter();
                    CH_DSM.ParameterName = "@CH_DSM"; CH_DSM.DbType = System.Data.DbType.String; CH_DSM.Direction = System.Data.ParameterDirection.Input; CH_DSM.Value = TLV;
                    System.Data.OleDb.OleDbParameter IN_DSM = new System.Data.OleDb.OleDbParameter();
                    IN_DSM.ParameterName = "@IN_DSM"; IN_DSM.DbType = System.Data.DbType.Int32; IN_DSM.Direction = System.Data.ParameterDirection.Input; IN_DSM.Value = 0;
                    SP.Parameters.Add(IN_ERC);
                    SP.Parameters.Add(CH_ASM);
                    SP.Parameters.Add(IN_ASM);
                    SP.Parameters.Add(CH_DSM);
                    SP.Parameters.Add(IN_DSM);
                    try
                    {
                        SP.ExecuteNonQuery();//this always fails
                    }
                    catch
                    {
                        MessageBox.Show("error en SP.ExecuteNonQuery();");
                    }
                    conn.Close();
                    Console.WriteLine("IN_ERC: " + SP.Parameters["@IN_ERC"].Value);
                    Console.WriteLine("CH_ASM: " + SP.Parameters["@CH_ASM"].Value);
                    Console.WriteLine("IN_ASM: " + SP.Parameters["@IN_ASM"].Value);

                    return SP.Parameters["@CH_ASM"].Value.ToString();
                }
                catch (Exception err)
                {
                    return null;
                }
            }
        }
        else
        {
            using (SqlConnection conn = new SqlConnection(BD))
            {
                try
                {
                    conn.Open();
                    SqlCommand SP = new SqlCommand();
                    SP.Connection = conn;
                    SP.CommandType = System.Data.CommandType.StoredProcedure;
                    SP.CommandText = SPName;
                    SqlParameter IN_ERC = new SqlParameter();
                    IN_ERC.ParameterName = "@IN_ERC"; IN_ERC.SqlDbType = System.Data.SqlDbType.Int; IN_ERC.Direction = System.Data.ParameterDirection.Output; IN_ERC.Value = 0;
                    SqlParameter CH_ASM = new SqlParameter();
                    CH_ASM.ParameterName = "@CH_ASM"; CH_ASM.SqlDbType = System.Data.SqlDbType.NVarChar; CH_ASM.Direction = System.Data.ParameterDirection.Output; CH_ASM.Value = 0; CH_ASM.Size = 1000;
                    SqlParameter IN_ASM = new SqlParameter();
                    IN_ASM.ParameterName = "@IN_ASM"; IN_ASM.SqlDbType = System.Data.SqlDbType.Int; IN_ASM.Direction = System.Data.ParameterDirection.Output; IN_ASM.Value = 0;
                    SqlParameter CH_DSM = new SqlParameter();
                    CH_DSM.ParameterName = "@CH_DSM"; CH_DSM.SqlDbType = System.Data.SqlDbType.NVarChar; CH_DSM.Direction = System.Data.ParameterDirection.Input; CH_DSM.Value = TLV;
                    SqlParameter IN_DSM = new SqlParameter();
                    IN_DSM.ParameterName = "@IN_DSM"; IN_DSM.SqlDbType = System.Data.SqlDbType.Int; IN_DSM.Direction = System.Data.ParameterDirection.Input; IN_DSM.Value = 0;
                    SP.Parameters.Add(IN_ERC);
                    SP.Parameters.Add(CH_ASM);
                    SP.Parameters.Add(IN_ASM);
                    SP.Parameters.Add(CH_DSM);
                    SP.Parameters.Add(IN_DSM);
                    try
                    {
                        SP.ExecuteNonQuery();//this always works
                    }
                    catch
                    {
                        MessageBox.Show("error en SP.ExecuteNonQuery();");
                    }
                    conn.Close();
                    Console.WriteLine("IN_ERC: " + SP.Parameters["@IN_ERC"].Value);
                    Console.WriteLine("CH_ASM: " + SP.Parameters["@CH_ASM"].Value);
                    Console.WriteLine("IN_ASM: " + SP.Parameters["@IN_ASM"].Value);

                    return SP.Parameters["@CH_ASM"].Value.ToString();
                }
                catch (Exception err)
                {
                    return null;
                }
            }
        }
    }

我使用以下参数调用该函数:

someString = myFunction(OPN_ASM, "OPN_ASM", BD);

根据mdbEnabled.Checked属性值设置BD变量,它可以是:

C:\Users\Angcrad\Documents\Visual Studio 2015\Projects\myDB.mdb

如果mdbEnabled.Checked == true

Data Source=ANGCRAD\SQLEXPRESS;Initial Catalog=myDB;Integrated Security=True

如果mdbEnabled.Checked == false

" myDB.mdb"文件是从SQL Server上的myDB数据库导出的,因此两个数据库都具有相同的元素。希望你能帮助我追踪错误,提前谢谢

0 个答案:

没有答案