【问题标题】:(OleDb) ExecuteNonQuery error - Type name is invalid(OleDb) ExecuteNonQuery 错误 - 类型名称无效
【发布时间】:2011-02-22 11:42:51
【问题描述】:

我在编写 asp.net 代码方面非常新,但我无法解决问题。每次我尝试注册时都会收到以下错误:

Type name is invalid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Type name is invalid.

Source Error:

Line 60:             }
Line 61: 
Line 62:             cmnd.ExecuteNonQuery();
Line 63:             this.cnct.Close();
Line 64:         }

源码为:

    public class Connection
    {
        private OleDbConnection cnct;

        public Connection(string dbPath)
        {
            this.cnct = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + dbPath);
        }

        /// <param name="types">'c' - VarChar, 'C' - Char, 'n' - VarNumeric, 'b' - Boolean</param>
        public void Insert(string tableName, string[] inputs, char[] types)
        {
            string sql = "INSERT INTO " + tableName + " VALUES (";
            for (int i = 0; i < inputs.Length - 1; i++)
                sql += "@p" + i + ",";
            sql += "@p" + (inputs.Length - 1) + ")";

            this.cnct.Open();
            OleDbCommand cmnd = new OleDbCommand(sql, this.cnct);

            for (int i = 0; i < inputs.Length; i++)
            {
                if (types[i] == 'c')
                {
                    cmnd.Parameters.Add("@p" + i, OleDbType.VarChar);
                    cmnd.Parameters["@p" + i].Value = inputs[i];
                }
                else if (types[i] == 'C')
                {
                    cmnd.Parameters.Add("@p" + i, OleDbType.Char);
                    cmnd.Parameters["@p" + i].Value = inputs[i];
                }
                else if (types[i] == 'n')
                {
                    cmnd.Parameters.Add("@p" + i, OleDbType.VarNumeric);
                    cmnd.Parameters["@p" + i].Value = double.Parse(inputs[i]);
                }
                else if (types[i] == 'b')
                {
                    cmnd.Parameters.Add("@p" + i, OleDbType.Boolean);
                    cmnd.Parameters["@p" + i].Value = bool.Parse(inputs[i]);
                }
            }

            cmnd.ExecuteNonQuery();
            this.cnct.Close();
        }
    }

Insert 方法被调用

Connection cnct = new Connection(Server.MapPath("App_Data/WMUdb.mdb"));
cnct.Insert("members", values, new char[7] { 'c', 'c', 'c', 'c', 'C', 'n', 'b' });

(在哪里

values = new string[7] {"userName","my name","pswrd123","email@example.com","2000-01-01"};

)。

我应该怎么做才能解决它?

非常感谢。

【问题讨论】:

    标签: c# asp.net oledb executenonquery


    【解决方案1】:

    Jet 引擎的 OLEDB 提供程序似乎不支持OleDbType.VarNumeric

    尝试改用OleDbType.Numeric

    【讨论】:

      【解决方案2】:

      另外,我闻到了日期列...“2000-01-01”};确保您的表列数据类型是您期望的字符,或者它真的是日期/时间列。如果是这样,请使用 DateTime 参数类型并确保将参数值正确设置为日期/时间值,即使它设置为相关日期 2000 年 1 月 1 日凌晨 12:00:00。

      【讨论】:

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