3

我从 Oracle 数据库中检索数据并填充网格视图。接下来,我尝试运行查询以选择一些数据,但出现错误。这是代码:

数据库:

public static OracleConnection GetConnection()
{
    OracleConnection connection = null;

    string connectionString = "Data Source=" + Database +
        ";User ID=" + UserID +
        ";Password=" + Password +
        ";Unicode=True";

    try
    {
        connection = new OracleConnection(connectionString);
    }
    catch (OracleException ex)
    {
        throw ex;
    }

    return connection;
}

参数从 default.aspx.cs 发送:

new Db(database, userID, password);
OracleConnection connection = Db.GetConnection();

main.aspx.cs 检索所有数据:

private OracleConnection connection = new OracleConnection();
private Select select = new Select();

protected void Page_Load(object sender, EventArgs e)
{
    Response.Buffer = true;

    if (Db.IsLoggedIn())
    {
        string selectCommand = 
           "SELECT " + Settings.TABLE + ".* FROM " + Settings.TABLE + " ORDER BY ";
        foreach (string ob in Settings.OB) selectCommand += ob + ", ";

        Session["Error"] = null;
        connection = Db.GetConnection();

        select = new Select(ddlBubID, ddlBusArea, ddlDrillSite, ddlWell, connection);

        gvData.DataKeyNames = Settings.PK;
        gvData.SelectedIndex = -1;

        DS.ConnectionString = connection.ConnectionString;
        DS.SelectCommand = selectCommand.Remove(selectCommand.Length - 2, 2);
        DS.ProviderName = Settings.PROVIDER_NAME;

        PopulateFooter(gvData.FooterRow);
    }
    else
    {
        Session["Error"] = Settings.ERROR_MESSAGE[0, 0];
        Response.Clear();
        Response.Redirect("default.aspx");
    }
}

public string ToolTip(string column)
{
    string value = "";
    OracleCommand cmd = new OracleCommand();
    cmd.Connection = connection;
    cmd.CommandText = "SELECT DISTINCT COMMENTS " +
                      "FROM SYS.ALL_COL_COMMENTS " +
                      "WHERE (TABLE_NAME = 'CTD_PROBLEM_EDIT_V') " +
                      "AND (COLUMN_NAME = " + column + ")";
    cmd.CommandType = CommandType.Text;
    OracleDataReader reader = cmd.ExecuteReader(); // I get an error here
    reader.Read();
        value = reader["COMMENTS"].ToString();
    reader.Close();
    return value;
}

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
        for (int i = 1; i < e.Row.Cells.Count; i++)
        {
            try
            {
                LinkButton lb =
                    (LinkButton)gvData.HeaderRow.Cells[i].Controls[0];
                lb.ToolTip = ToolTip(lb.Text);

                /* Blah Blah*/
            }
            catch { }

        }

    if (e.Row.RowType == DataControlRowType.Footer)
        PopulateFooter(e.Row);
}

工具提示();抛出错误:无效操作。连接已关闭。

编辑:

这会很有帮助: 静态类和静态类成员

4

2 回答 2

3

可能不是问题,但这看起来很奇怪:

new Db(database, userID, password);
OracleConnection connection = Db.GetConnection();

GetConnection是一个静态方法,因此它看不到您可能在构造函数中设置的任何成员属性(除非它们也是静态的)。如果它们都是静态的,请考虑重构代码以使用单例模式,因为它更具可读性。

另一件事是连接属性是为每个请求(而不是每个应用程序)生成的页面类的成员。这意味着您需要在ToolTip方法(以及访问数据库的任何其他方法)中创建一个新连接,或者将connection属性设为静态以使其适用于每个应用程序。

于 2011-07-18T20:37:19.787 回答
1

尝试两件事:

1.. 对于您的ToolTip()方法,column比较 COLUMN_NAME 的值需要用表示字符串/varchar 文字值的单引号正确包裹。可能它正在评估 COLUMN_NAME = foo它应该是什么时候COLUMN_NAME = 'foo'

cmd.CommandText = "SELECT DISTINCT COMMENTS " +                      
"FROM SYS.ALL_COL_COMMENTS " +                      
"WHERE (TABLE_NAME = 'CTD_PROBLEM_EDIT_V') " +                      
"AND (COLUMN_NAME = '" + column + "')";

2.. 尝试将您的即席 SQL 语句包装在BEGINEND

SELECT3..考虑为您的和动态ORDER BY子句重构您的字符串构建。您在下面的许多行中对 SelectCommand 执行此操作对于后来的临时观察者或维护者来说并不明显。

 string selectCommand = string.Format("SELECT {0}.* FROM {0} ORDER BY {1}"
                                       ,Settings.TABLE 
                                       ,string.Join(",",Settings.OB));
于 2011-07-18T20:50:38.300 回答