Example #1
0
        private bool IsColumnReadOnly(string name, string column)
        {
            if (radioSql.Checked)
            {
                int  tableid = Gateway.Default.SelectScalar <int>("select id from sysobjects where [name] = @name", new object[] { name });
                byte status  = Gateway.Default.SelectScalar <byte>("select status from syscolumns where [name] = @name and id = @id", new object[] { column, tableid });
                return(status == 128);
            }
            else if (radioOracle.Checked)
            {
                return(false);
            }
            else if (radioMySql.Checked)
            {
                System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("(^.*database=)([^;]+)(;.*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                string dbName = r.Replace(txtConnStr.Text, "$2").ToLower();

                DataSet ds = Gateway.Default.SelectDataSet("select EXTRA from COLUMNS where TABLE_SCHEMA = '" + dbName + "' and COLUMN_NAME = '" + column + "' and TABLE_NAME = ?TABLE_NAME", new object[] { name });
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i][0].ToString() == "auto_increment")
                    {
                        return(true);
                    }
                }

                return(false);
            }
            else
            {
                ADODB.ConnectionClass conn = new ADODB.ConnectionClass();
                conn.Provider = "Microsoft.Jet.OLEDB.4.0";
                string connStr = txtConnStr.Text;
                conn.Open(connStr.Substring(connStr.ToLower().IndexOf("data source") + "data source".Length).Trim('=', ' '), null, null, 0);

                ADODB.Recordset rs = conn.GetType().InvokeMember("OpenSchema", BindingFlags.InvokeMethod, null, conn, new object[] { ADODB.SchemaEnum.adSchemaColumns }) as ADODB.Recordset;
                rs.Filter = "TABLE_NAME='" + name + "'";

                while (!rs.EOF)
                {
                    if ((rs.Fields["COLUMN_NAME"].Value as string) == column && ((int)rs.Fields["DATA_TYPE"].Value) == 3 && Convert.ToByte(rs.Fields["COLUMN_FLAGS"].Value) == 90)
                    {
                        return(true);
                    }

                    rs.MoveNext();
                }
            }

            return(false);
        }
Example #2
0
        private bool IsColumnPrimaryKey(string name, string column)
        {
            if (radioSql.Checked)
            {
                int     tableid = Gateway.Default.SelectScalar <int>("select id from sysobjects where [name] = @name", new object[] { name });
                DataSet ds      = Gateway.Default.SelectDataSet("select a.name FROM syscolumns a inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' where (SELECT count(*) FROM sysobjects WHERE (name in (SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in (SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name))))))) AND (xtype = 'PK'))>0 and d.id = @id", new object[] { tableid });
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i][0].ToString() == column)
                    {
                        return(true);
                    }
                }
            }
            else if (radioOracle.Checked)
            {
                DataSet ds = Gateway.Default.SelectDataSet("select b.COLUMN_NAME from USER_CONSTRAINTS a,USER_CONS_COLUMNS b where a.CONSTRAINT_NAME=b.CONSTRAINT_NAME and a.table_name=b.table_name and constraint_type='P' and a.owner=b.owner and a.table_name = :name", new object[] { name });
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i][0].ToString() == column)
                    {
                        return(true);
                    }
                }
            }
            else if (radioMySql.Checked)
            {
                System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("(^.*database=)([^;]+)(;.*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                string dbName = r.Replace(txtConnStr.Text, "$2").ToLower();

                DataSet ds = Gateway.Default.SelectDataSet("select COLUMN_NAME from KEY_COLUMN_USAGE where CONSTRAINT_SCHEMA = '" + dbName + "' and CONSTRAINT_NAME = 'PRIMARY' and TABLE_NAME = ?TABLE_NAME", new object[] { name });
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i][0].ToString() == column)
                    {
                        return(true);
                    }
                }
            }
            else
            {
                ADODB.ConnectionClass conn = new ADODB.ConnectionClass();
                conn.Provider = "Microsoft.Jet.OLEDB.4.0";
                string connStr = txtConnStr.Text;
                conn.Open(connStr.Substring(connStr.ToLower().IndexOf("data source") + "data source".Length).Trim('=', ' '), null, null, 0);

                ADODB.Recordset rs = conn.GetType().InvokeMember("OpenSchema", BindingFlags.InvokeMethod, null, conn, new object[] { ADODB.SchemaEnum.adSchemaPrimaryKeys }) as ADODB.Recordset;
                rs.Filter = "TABLE_NAME='" + name + "'";

                while (!rs.EOF)
                {
                    if ((rs.Fields["COLUMN_NAME"].Value as string) == column)
                    {
                        return(true);
                    }

                    rs.MoveNext();
                }
            }

            return(false);
        }
Example #3
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (txtConnStr.Text.Trim().Length == 0)
            {
                MessageBox.Show("Connection string cannot be null!");
                return;
            }

            if (btnConnect.Text == "Disconnect")
            {
                EnableGenEntity(false);
                return;
            }

            RefreshConnectionStringAutoComplete();

            DataSet dsTables = null;
            DataSet dsViews  = null;

            if (radioSql.Checked || radioAccess.Checked)
            {
                try
                {
                    if (radioSql.Checked)
                    {
                        Gateway.SetDefaultDatabase(DatabaseType.SqlServer, txtConnStr.Text);
                        if (checkSql2005.Checked)
                        {
                            dsTables = Gateway.Default.SelectDataSet("select [name] from sysobjects where xtype = 'U' and [name] <> 'sysdiagrams' order by [name]", null);
                        }
                        else
                        {
                            dsTables = Gateway.Default.SelectDataSet("select [name] from sysobjects where xtype = 'U' and status > 0 order by [name]", null);
                        }
                        foreach (DataRow row in dsTables.Tables[0].Rows)
                        {
                            tables.Items.Add(row["Name"].ToString());
                        }

                        if (checkSql2005.Checked)
                        {
                            dsViews = Gateway.Default.SelectDataSet("select [name] from sysobjects where xtype = 'V' order by [name]", null);
                        }
                        else
                        {
                            dsViews = Gateway.Default.SelectDataSet("select [name] from sysobjects where xtype = 'V' and status > 0 order by [name]", null);
                        }
                        foreach (DataRow row in dsViews.Tables[0].Rows)
                        {
                            views.Items.Add(row["Name"].ToString());
                        }
                    }
                    else if (radioAccess.Checked)
                    {
                        Gateway.SetDefaultDatabase(DatabaseType.MsAccess, txtConnStr.Text);
                        ADODB.ConnectionClass conn = new ADODB.ConnectionClass();
                        conn.Provider = "Microsoft.Jet.OLEDB.4.0";
                        string connStr = txtConnStr.Text;
                        conn.Open(connStr.Substring(connStr.ToLower().IndexOf("data source") + "data source".Length).Trim('=', ' '), null, null, 0);

                        ADODB.Recordset rsTables = conn.GetType().InvokeMember("OpenSchema", BindingFlags.InvokeMethod, null, conn, new object[] { ADODB.SchemaEnum.adSchemaTables }) as ADODB.Recordset;
                        ADODB.Recordset rsViews  = conn.GetType().InvokeMember("OpenSchema", BindingFlags.InvokeMethod, null, conn, new object[] { ADODB.SchemaEnum.adSchemaViews }) as ADODB.Recordset;

                        while (!rsViews.EOF)
                        {
                            if (!(rsViews.Fields["TABLE_NAME"].Value as string).StartsWith("MSys"))
                            {
                                views.Items.Add(rsViews.Fields["TABLE_NAME"].Value.ToString());
                            }
                            rsViews.MoveNext();
                        }

                        while (!rsTables.EOF)
                        {
                            if (!(rsTables.Fields["TABLE_NAME"].Value as string).StartsWith("MSys"))
                            {
                                bool isView = false;
                                foreach (string item in views.Items)
                                {
                                    if (item.Equals(rsTables.Fields["TABLE_NAME"].Value.ToString()))
                                    {
                                        isView = true;
                                        break;
                                    }
                                }
                                if (!isView)
                                {
                                    tables.Items.Add(rsTables.Fields["TABLE_NAME"].Value.ToString());
                                }
                            }
                            rsTables.MoveNext();
                        }

                        rsTables.Close();
                        rsViews.Close();

                        conn.Close();
                    }

                    EnableGenEntity(true);
                }
                catch (Exception ex)
                {
                    EnableGenEntity(false);
                    MessageBox.Show("Read/write database error!\r\n" + ex.ToString());
                }
            }
            else if (radioOracle.Checked)
            {
                Gateway.SetDefaultDatabase(DatabaseType.Oracle, txtConnStr.Text);

                dsTables = Gateway.Default.SelectDataSet("select * from user_tables where global_stats = 'NO' and (not table_name like '%$%')", null);
                foreach (DataRow row in dsTables.Tables[0].Rows)
                {
                    tables.Items.Add(row["TABLE_NAME"].ToString());
                }

                dsViews = Gateway.Default.SelectDataSet("select * from user_views where (not view_name like '%$%') and (not view_name like 'MVIEW_%') and (not view_name like 'CTX_%') and (not view_name = 'PRODUCT_PRIVS')", null);
                foreach (DataRow row in dsViews.Tables[0].Rows)
                {
                    views.Items.Add(row["VIEW_NAME"].ToString());
                }

                EnableGenEntity(true);
            }
            else if (radioMySql.Checked)
            {
                System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("(^.*database=)([^;]+)(;.*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                string dbName = r.Replace(txtConnStr.Text, "$2").ToLower();
                Gateway.SetDefaultDatabase(DatabaseType.MySql, r.Replace(txtConnStr.Text, "$1information_schema$3"));

                dsTables = Gateway.Default.SelectDataSet("select * from TABLES where TABLE_TYPE = 'BASE TABLE' and TABLE_SCHEMA = '" + dbName + "'", null);
                foreach (DataRow row in dsTables.Tables[0].Rows)
                {
                    tables.Items.Add(row["TABLE_NAME"].ToString());
                }

                dsViews = Gateway.Default.SelectDataSet("select * from TABLES where TABLE_TYPE = 'VIEW' and TABLE_SCHEMA = '" + dbName + "'", null);
                foreach (DataRow row in dsViews.Tables[0].Rows)
                {
                    views.Items.Add(row["TABLE_NAME"].ToString());
                }

                EnableGenEntity(true);
            }
            else
            {
                EnableGenEntity(false);
                MessageBox.Show("EntityGen tool only supports SqlServer, MsAccess, MySql and Oracle Database!");
            }
        }