oracle 监测数据库是否存在指定字段

 public static bool ExistColumn(string tableName, string columnName, string connStr)
        {
            using (OracleConnection conn = new OracleConnection(connStr))
            {
                try
                {
                    bool blnRet = false;
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "select count(1) from USER_TAB_COLUMNS where table_name=:tablename and column_name=:columnname";
                    OracleParameter param = cmd.Parameters.Add(":tablename", OracleDbType.Varchar2);
                    param.Value = tableName.ToUpper();
                    param = cmd.Parameters.Add(":columnname", OracleDbType.Varchar2);
                    param.Value = columnName.ToUpper();
                    object obj = cmd.ExecuteScalar();
                    if (obj != null && obj != DBNull.Value)
                    {
                        if (Convert.ToInt64(obj) > 0)
                        {
                            blnRet = true;
                        }
                    }
                    conn.Close();
                    return blnRet;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
        }

 

posted @ 2015-06-03 15:21  洛晨随风  阅读(259)  评论(0编辑  收藏  举报