簡體   English   中英

(OleDb)ExecuteNonQuery錯誤-類型名稱無效

[英](OleDb) ExecuteNonQuery error - Type name is invalid

我在編寫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"};

)。

我該怎么解決?

非常感謝。

看來Jet引擎的OLEDB提供程序不支持OleDbType.VarNumeric

嘗試改用OleDbType.Numeric

另外,我正在聞日期列...“ 2000-01-01”}; 確保表列的數據類型符合您的期望,或者它確實是日期/時間列。 如果是這樣,則將其參數類型設置為DateTime並確保將參數值正確設置為日期/時間值,即使該參數值設置為所討論的2000-01-01日期的12:00:00 am也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM