/// <summary> /// Loads the given database object. /// </summary> /// <param name="obj">The object to load.</param> /// <returns>Returns true if successfully loaded; false if not.</returns> public bool Load(ISqlDataObject obj) { using (SqlDatabaseClient client = GetClient()) { return(obj.Load(client)); } }
/// <summary> /// Updates the given database object. /// </summary> /// <param name="obj">The object to update.</param> /// <returns>Returns true if successfully updated; false if not.</returns> public bool Update(ISqlDataObject obj) { using (SqlDatabaseClient client = GetClient()) { return(obj.Update(client)); } }
/// <summary> /// Inserts the given database object. /// </summary> /// <param name="obj">The object to delete.</param> /// <returns>Returns true if successfully deleted; false if not.</returns> public bool Insert(ISqlDataObject obj) { using (SqlDatabaseClient client = GetClient()) { return(obj.Insert(client)); } }
public static void CreateTableFromDefinition(ISqlDataObject templateTable, SqlDatabaseConnection connection) { var createSql = GetCreateSql(templateTable); if (connection != null) { using (var cmd = new SqlDatabaseCommand(createSql, connection)) { cmd.ExecuteNonQuery(); } } }
public void InitializeData(ISqlDataObject table) { try { var sql = "SELECT * FROM " + table.Name + " LIMIT " + pageSize + " OFFSET " + start; var sqlCount = "SELECT COUNT(*) FROM " + table.Name; this.Text = table.Name; connection = new SqlDatabaseConnection(table.ConnectionString); connection.Open(); this.table = new DataTable(table.Name); command = new SqlDatabaseCommand(sql, connection); adapter = new SqlDatabaseDataAdapter(command); adapter.Fill(this.table); total = Convert.ToInt32(new SqlDatabaseCommand(sqlCount, connection).ExecuteScalar()); sourceTable = table; hasPrimaryKey = table.Columns.Any(o => o.IsPKey); btnCreatePKey.Enabled = !hasPrimaryKey; btnCreatePKey.ToolTipText = hasPrimaryKey ? "Table has existent primary key" : "Recreate Table with primary Key"; btnCreatePKey.Visible = !hasPrimaryKey; dataGridView1.AllowUserToResizeRows = false; dataGridView1.DataSource = this.table; if (table.GetType() == typeof(SqlDataView)) { dataGridView1.ReadOnly = true; dataGridView1.AllowUserToAddRows = false; dataGridView1.AllowUserToDeleteRows = false; } UpdateRecordsInfo(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error loading Data", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static string GetCreateSql(ISqlDataObject templateTable) { var createSql = "DROP TABLE IF EXISTS " + templateTable.Name + ";\n" + "CREATE TABLE " + templateTable.Name + "\n("; if (templateTable.GetType() == typeof(SqlDataView)) { createSql = "DROP VIEW IF EXISTS " + templateTable.Name + ";\r\n"; using (var con = new SqlDatabaseConnection(templateTable.ConnectionString)) { con.Open(); try { var sql = string.Format("SELECT sqltext FROM SYS_OBJECTS WHERE\n" + "type = 'view' \n" + "AND name = '{0}'", templateTable.Name); using (var cmd = new SqlDatabaseCommand(sql, con)) { createSql += Convert.ToString(cmd.ExecuteScalar()); } } catch { throw; } finally { con.Close(); } } return(createSql); } var fields = new List <string>(); foreach (var col in templateTable.Columns.Where(o => !string.IsNullOrEmpty(o.Name))) { var s = "\n" + col.Name + " " + col.Type; if (!col.Nullable || col.IsPKey) { s += " NOT NULL"; } if (col.IsPKey) { s += " PRIMARY KEY"; } if (col.IsPKey && col.AutoInc) { s += " AUTOINCREMENT "; } var sDefault = Convert.ToString(col.DefaultValue); if (sDefault == "''") { sDefault = string.Empty; } if (!col.AutoInc && col.DefaultValue != null && col.DefaultValue != DBNull.Value && !string.IsNullOrEmpty(sDefault)) { s += " DEFAULT "; if (col.Type.ToUpper() == "TEXT" && !sDefault.StartsWith("'")) { sDefault = "'" + sDefault + "'"; } s += sDefault; } fields.Add(s); } createSql += string.Join(",", fields); createSql += "\n)"; return(createSql.Replace("\n", "\r\n")); }
public ChooseKeyColumnsForm(ISqlDataObject table) : this() { Table = table; }