static internal List <SqlSchemaInfo> GetSchemaInfo(System.Data.Odbc.OdbcConnection con, List <SqlSchemaInfo> schemaList) { try { DataTable tbl = con.GetSchema(System.Data.Odbc.OdbcMetaDataCollectionNames.Tables); foreach (DataRow row in tbl.Rows) { SqlSchemaInfo ssi = new SqlSchemaInfo(); ssi.Type = "TABLE"; ssi.Name = (string)row["table_name"]; schemaList.Add(ssi); } tbl = con.GetSchema(System.Data.Odbc.OdbcMetaDataCollectionNames.Views); foreach (DataRow row in tbl.Rows) { SqlSchemaInfo ssi = new SqlSchemaInfo(); ssi.Type = "VIEW"; ssi.Name = (string)row["table_name"]; schemaList.Add(ssi); } } catch { } schemaList.Sort(); return(schemaList); }
static internal List <SqlSchemaInfo> GetSchemaInfo(System.Data.SqlClient.SqlConnection con, List <SqlSchemaInfo> schemaList) { try { DataTable tbl = con.GetSchema(System.Data.SqlClient.SqlClientMetaDataCollectionNames.Tables); foreach (DataRow row in tbl.Rows) { SqlSchemaInfo ssi = new SqlSchemaInfo(); ssi.Type = "TABLE"; string schema = row["table_schema"] as string; if (schema != null && schema != "dbo") { ssi.Name = string.Format("{0}.{1}", schema, (string)row["table_name"]); } else { ssi.Name = (string)row["table_name"]; } schemaList.Add(ssi); } tbl = con.GetSchema(System.Data.SqlClient.SqlClientMetaDataCollectionNames.Views); foreach (DataRow row in tbl.Rows) { SqlSchemaInfo ssi = new SqlSchemaInfo(); ssi.Type = "VIEW"; string schema = row["table_schema"] as string; if (schema != null && schema != "dbo") { ssi.Name = string.Format("{0}.{1}", schema, (string)row["table_name"]); } else { ssi.Name = (string)row["table_name"]; } schemaList.Add(ssi); } } catch { } schemaList.Sort(); return(schemaList); }
static internal List <SqlSchemaInfo> GetSchemaInfo(string dataProvider, string connection) { List <SqlSchemaInfo> schemaList = new List <SqlSchemaInfo>(); IDbConnection cnSQL = null; IDbCommand cmSQL = null; IDataReader dr = null; Cursor saveCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; // Get the schema information try { int ID_TABLE = 0; int ID_TYPE = 1; // Open up a connection cnSQL = RdlEngineConfig.GetConnection(dataProvider, connection); if (cnSQL == null) { MessageBox.Show(string.Format("Unable to connect using dataProvider '{0}'", dataProvider), "SQL Error"); return(schemaList); } cnSQL.Open(); // Take advantage of .Net metadata if available if (cnSQL is System.Data.SqlClient.SqlConnection) { return(GetSchemaInfo((System.Data.SqlClient.SqlConnection)cnSQL, schemaList)); } if (cnSQL is System.Data.Odbc.OdbcConnection) { return(GetSchemaInfo((System.Data.Odbc.OdbcConnection)cnSQL, schemaList)); } if (cnSQL is System.Data.OleDb.OleDbConnection) { return(GetSchemaInfo((System.Data.OleDb.OleDbConnection)cnSQL, schemaList)); } // Obtain the query needed to get table/view list string sql = RdlEngineConfig.GetTableSelect(dataProvider, cnSQL); if (sql == null || sql.Length == 0) // when no query string; no meta information available { return(schemaList); } // Obtain the query needed to get table/view list cmSQL = cnSQL.CreateCommand(); cmSQL.CommandText = sql; dr = cmSQL.ExecuteReader(); string type = "TABLE"; while (dr.Read()) { SqlSchemaInfo ssi = new SqlSchemaInfo(); if (ID_TYPE >= 0 && dr.FieldCount < ID_TYPE && (string)dr[ID_TYPE] == "VIEW") { type = "VIEW"; } ssi.Type = type; ssi.Name = (string)dr[ID_TABLE]; schemaList.Add(ssi); } } catch (SqlException sqle) { MessageBox.Show(sqle.Message, "SQL Error"); } catch (Exception e) { MessageBox.Show(e.InnerException == null? e.Message: e.InnerException.Message, "Error"); } finally { if (cnSQL != null) { cnSQL.Close(); if (cmSQL != null) { cmSQL.Dispose(); } if (dr != null) { dr.Close(); } } Cursor.Current = saveCursor; } return(schemaList); }