ORA-01036: 非法变量名/编号

发布于 2024-11-24 03:11:48 字数 3067 浏览 2 评论 0原文

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

Db.cs:

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);
}

ToolTip();抛出错误: 无效操作。连接已关闭。

编辑:

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

I retrieve data from Oracle database and populate a gridview. Next, I try to run a query to select some data but I get an error.
Here is the code:

Db.cs:

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;
}

Parameters are sent from default.aspx.cs:

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

main.aspx.cs retrieves all the data:

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);
}

ToolTip(); throws an error:
Invalid operation. The connection is closed.

EDIT:

This would have been helpful:
Static Classes and Static Class Members

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-12-01 03:11:49

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

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

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

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

Might not be the problem but this looks weird:

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

GetConnection is a static method and thus it does not see any member attributes you might be setting in the constructor (unless they are static as well). If they are all static, consider refactoring your code to use the singleton pattern as it is more readable.

Another thing is that the connection attribute is a member of the page class which is generated for each request (not per application). This means you need either create a new connection in ToolTip method (and any other method that accesses the database) or make the connection attribute static to make it per-application.

萌能量女王 2024-12-01 03:11:49

尝试 2 件事:

1.. 对于您的 ToolTip() 方法,用于比较 COLUMN_NAME 的值 column 需要用单引号正确括起来,表示字符串/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

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

 string selectCommand = string.Format("SELECT {0}.* FROM {0} ORDER BY {1}"
                                       ,Settings.TABLE 
                                       ,string.Join(",",Settings.OB));

Try 2 things:

1.. For your ToolTip() method, the value column to compare for COLUMN_NAME will need to be wrapped properly with single quotes indicating a string/varchar literal value. Likely it's evaluating to COLUMN_NAME = foo when it should be 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.. Try wrapping your ad-hoc SQL statements in BEGIN and END

3.. Consider refactoring your string building for your SELECT and dynamic ORDER BY clause. That you're doing it on the SelectCommand many lines below isn't obvious to the casual observer or maintainers later in its life.

 string selectCommand = string.Format("SELECT {0}.* FROM {0} ORDER BY {1}"
                                       ,Settings.TABLE 
                                       ,string.Join(",",Settings.OB));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文