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

时间:2011-07-14 21:43:14

标签: c# asp.net gridview select oraclecommand

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

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

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

修改

这会有所帮助:   Static Classes and Static Class Members

2 个答案:

答案 0 :(得分:3)

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

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

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

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

答案 1 :(得分:1)

尝试两件事:

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 ..尝试在BEGINEND

中包装您的临时SQL语句

3 ..考虑为您的SELECT和动态ORDER BY子句重构字符串构建。你在SelectCommand上做的这么多行对于后来的观察者或维护人员来说并不明显。

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