public bool IsExistDataBase(string databaseName) { var sql = string.Format($"SELECT COUNT(*) FROM master.dbo.sysdatabases WHERE name = '{databaseName}'"); var sqlAccess = SqlAccessFactory.CreateSqlAccess(DataBaseManager.Instance.SqlType, DataBaseManager.Instance.ConnectString); var count = SqlTyepConvert.ConvertToInt(sqlAccess.ExecuteScalar(sql)); return(count > 0); }
public bool Insert(ModelBase model) { using (var sqlAccess = SqlAccessFactory.CreateSqlAccess(DataBaseManager.Instance.SqlType, DataBaseManager.Instance.ConnectString)) { var sql = CreateInsertSql(model); return(sqlAccess.ExecuteNonQuery(sql) > 0); } }
public ModelBase Search(string modelName, int id) { var modelObj = CreateInstance(modelName); if (modelObj == null) { throw new ArgumentException("获取实体表不存在"); } using (var sqlAccess = SqlAccessFactory.CreateSqlAccess(DataBaseManager.Instance.SqlType, DataBaseManager.Instance.ConnectString)) { var sql = CreateSelectSql(modelObj, id, false); var dt = sqlAccess.ExecuteDataTable(sql); if (dt.Rows.Count == 0) { return(null); } var firstRow = dt.Rows[0]; foreach (DataColumn col in dt.Columns) { if (col.ColumnName == "Id") { SetProperty(modelObj, "Id", firstRow[col]); } else { var colPro = modelObj.GetType().GetProperty(col.ColumnName); if (colPro.PropertyType == typeof(Many2One) || colPro.PropertyType == typeof(Many2Many) || colPro.PropertyType == typeof(One2Many)) { if (firstRow[col] == DBNull.Value) { continue; } if (colPro.PropertyType == typeof(Many2One)) { dynamic m2o = colPro.GetValue(modelObj); } else if (colPro.PropertyType == typeof(One2Many)) { } } else { SetColumnProperty(modelObj, col.ColumnName, firstRow[col]); } } } } return(modelObj); }
public bool Delete(string modelName, int id) { var modelObj = CreateInstance(modelName); using (var sqlAccess = SqlAccessFactory.CreateSqlAccess(DataBaseManager.Instance.SqlType, DataBaseManager.Instance.ConnectString)) { var delSql = CreateDeleteSql(modelObj, id); return(sqlAccess.ExecuteNonQuery(delSql) > 0); } }
public bool Update(ModelBase model) { if (model == null || model.Id <= 0) { return(false); } using (var sqlAccess = SqlAccessFactory.CreateSqlAccess(DataBaseManager.Instance.SqlType, DataBaseManager.Instance.ConnectString)) { var sql = CreateUpdateSql(model); return(sqlAccess.ExecuteNonQuery(sql) > 0); } }
public bool CreateDataBase(string databaseName) { var sqlAccess = SqlAccessFactory.CreateSqlAccess(DataBaseManager.Instance.SqlType, DataBaseManager.Instance.ConnectString); var sql = string.Format($"CREATE DATABASE {databaseName}"); try { //sqlAccess.BeginTransaction(); sqlAccess.ExecuteNonQuery(sql); //sqlAccess.Commit(); DataBaseManager.Instance.DataBaseName = databaseName; CreateAllTables(); return(true); } catch { //sqlAccess.Rollback(); throw; } }
public void UpgradeAllTables() { var sqlAccess = SqlAccessFactory.CreateSqlAccess(DataBaseManager.Instance.SqlType, DataBaseManager.Instance.ConnectString); foreach (var keypair in ModelDic) { try { var model = UtilTool.ReflectorHelp.CreateInstance(keypair.Value) as ModelBase; if (model == null) { continue; } var sql = IsExistTable(model.TableName); if (SqlTyepConvert.ConvertToInt(sqlAccess.ExecuteScalar(sql)) == 0) {//不存在 sql = CreateTable(model.TableName); sqlAccess.ExecuteNonQuery(sql); } foreach (var column in model.GetColumnsField()) { dynamic sfield = column; if (!sfield.IsStore) { continue; } if (sfield is Many2Many || sfield is Many2One) { if (sfield is Many2Many) { sql = IsExistTable(model.TableName); if (SqlTyepConvert.ConvertToInt(sqlAccess.ExecuteScalar(sql)) == 0) {//不存在 sql = CreateRelationTable(sfield.RelationTableName, sfield.ColumnName, sfield.ColumnName1); } else { sql = string.Empty; } } else { sql = IsExistColumn(model.TableName, sfield.FieldName); if (SqlTyepConvert.ConvertToInt(sqlAccess.ExecuteScalar(sql)) == 0) { sql = CreateColumn(model.TableName, sfield.ColumnName, typeof(int), 0, 0); } else { sql = string.Empty; } } if (!string.IsNullOrEmpty(sql)) { sqlAccess.ExecuteNonQuery(sql); } } else { sql = IsExistColumn(model.TableName, sfield.FieldName); if (SqlTyepConvert.ConvertToInt(sqlAccess.ExecuteScalar(sql)) > 0) { sql = UpgradeColumn(model.TableName, sfield.FieldName, sfield.Type, sfield.Size, sfield.Precision); } else { sql = CreateColumn(model.TableName, sfield.FieldName, sfield.Type, sfield.Size, sfield.Precision); } sqlAccess.ExecuteNonQuery(sql); } } } catch (Exception e) { Console.WriteLine(e); throw; } } }