簡體   English   中英

web 中的“App_Code”文件夾無法正常工作

[英]My "App_Code" folder in my web isn't working

Bellow 是我在名為“MyDB.cs”的 App_Code 文件夾中的代碼。 我的 MyDB class 有問題。 當我嘗試運行代碼時,它會中斷並告訴我“con.Open”有問題。 如果有人知道問題出在哪里,它將對我有很大幫助。


using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Linq;

public class MyDB : IDisposable
{
    private readonly OleDbConnection con; 

    public MyDB()
    {
        string cs = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
        con = new OleDbConnection(cs);
        con.Open();
    }

    public DbCommand Command(string sql, params object[] values)
    {
        if (values.Length > 0)
            sql = string.Format(sql, values.Select(_ => " ? ").ToArray());
        OleDbCommand cmd = new OleDbCommand(sql, con);
        for (int i = 0; i < values.Length; i++)
            cmd.Parameters.AddWithValue("", values[i]);
        return cmd;
    }

    public void Dispose()
    {
        con.Dispose();
    }
}

這是錯誤消息:錯誤消息

嘗試這個

 public DbCommand Command(string sql, params object[] values)
{
    con.Open();
    if (values.Length > 0)
        sql = string.Format(sql, values.Select(_ => " ? ").ToArray());
    OleDbCommand cmd = new OleDbCommand(sql, con);
    for (int i = 0; i < values.Length; i++)
        cmd.Parameters.AddWithValue("", values[i]);
    return cmd;
    con.Close();
}

暫無
暫無

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

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