GetExist <T>(T ob) { if (String.IsNullOrEmpty(ConnectionString)) { ConnectionString = GlobalSqlContext.GetConStr(typeof(T), ""); } using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString)) { con.Open(); var gernerater = new MSSQL.MSSSqlCommandGenerater(); var command = gernerater.GenerateExitsSql <T>(ob); command.Connection = con; // int insertRowCount = command.ExecuteNonQuery(); command.Connection = con; SqlDataAdapter adapter = new SqlDataAdapter(command as SqlCommand); DataTable table = new DataTable(); T newob = (T)typeof(T).Assembly.CreateInstance(typeof(T).FullName); adapter.Fill(table); if (table.Rows.Count > 0) { SetObject(newob, table.Rows[0]); return(newob); } else { return(null); } } }
public object GetNextKey <T>(T ob) { if (String.IsNullOrEmpty(ConnectionString)) { ConnectionString = GlobalSqlContext.GetConStr(typeof(T), ""); } using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString)) { con.Open(); var gernerater = new MSSQL.MSSSqlCommandGenerater(); var command = gernerater.GenerateGetNextKey <T>(ob); command.Connection = con; SqlDataAdapter adapter = new SqlDataAdapter(command as SqlCommand); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { return(table.Rows[0][0]); } else { return(null); } } }
public List <I> Get <T, I> () where T : class, I, new() { var sqlCon = ConnectionString; if (string.IsNullOrEmpty(sqlCon)) { sqlCon = GlobalSqlContext.GetConStr(typeof(T), ""); } ConnectionString = sqlCon; using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString)) { con.Open(); var selectCommand = new MSSQL.MSSSqlCommandGenerater().GenerateSelectSql <T>(); selectCommand.Connection = con; var adapter = new System.Data.SqlClient.SqlDataAdapter(selectCommand as SqlCommand); DataTable table = new DataTable(); adapter.Fill(table); List <I> result = new List <I>(); var Type = typeof(T); var Properties = Type.GetProperties(); for (int i = 0; i < table.Rows.Count; i++) { var item = new T(); SetObject(item, table.Rows[i], Properties); result.Add(item); } return(result); } }
public List <T> GetItems <T>(object ob, string ParentProperty = null) { if (String.IsNullOrEmpty(ConnectionString)) { ConnectionString = GlobalSqlContext.GetConStr(typeof(T), ""); } using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString)) { var Type = typeof(T); var Properties = Type.GetProperties(); PropertyInfo parentInfo = null; if (ParentProperty == null) { foreach (var property in Properties) { var attrib = property.GetCustomAttributes(typeof(ParentRelationAttribute), true); if (attrib.Length > 0) { parentInfo = property; break; } else if (property.PropertyType == ob.GetType()) { parentInfo = property; } } } else { parentInfo = Type.GetProperty(ParentProperty); } con.Open(); var selectCommand = new MSSQL.MSSSqlCommandGenerater().GenerateSelectItemsSql <T>(ob, parentInfo); selectCommand.Connection = con; var adapter = new System.Data.SqlClient.SqlDataAdapter(selectCommand as SqlCommand); DataTable table = new DataTable(); adapter.Fill(table); List <T> result = new List <T>(); for (int i = 0; i < table.Rows.Count; i++) { var item = Type.Assembly.CreateInstance(Type.FullName); SetObject(item, table.Rows[i], Properties, true); parentInfo.SetValue(item, ob, null); result.Add((T)item); } return(result); } }
private void Delete <T>(T ob, bool Submit) { if (String.IsNullOrEmpty(ConnectionString)) { ConnectionString = GlobalSqlContext.GetConStr(typeof(T), ""); } var gernerater = new MSSQL.MSSSqlCommandGenerater(); // var command = gernerater.GenerateDeleteSql<T>(ob); var UpdateSqlCommand = gernerater.GenerateDeleteSql <T>(ob); this.TransmitCommands.Enqueue(new SqlTransAutoMic() { ob = ob, command = UpdateSqlCommand, Operation = Soway.DB.SqlOperation.delete }); if (Submit) { this.SubmitChanges(); } }
/// <summary> /// 新建 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ob"></param> private void Create <T>(T ob, bool Submit) { var sqlCon = ConnectionString; if (string.IsNullOrEmpty(sqlCon)) { sqlCon = GlobalSqlContext.GetConStr(typeof(T), ""); } ConnectionString = sqlCon; var gernerater = new MSSQL.MSSSqlCommandGenerater(); var command = gernerater.GenerateCreateSql <T>(ob, this); // var UpdateSqlCommand = gernerater.GenerateSeletedAutoGernerateSql<T>(ob); this.TransmitCommands.Enqueue(new SqlTransAutoMic() { ob = ob, command = command, Operation = SqlOperation.insert }); var UpdateSqlCommand = gernerater.GenerateSeletedAutoGernerateSql <T>(ob); if (UpdateSqlCommand != null) { this.TransmitCommands.Enqueue(new SqlTransAutoMic() { ob = ob, command = UpdateSqlCommand, Operation = SqlOperation.updateafterinsert }); } if (Submit) { this.SubmitChanges(); } }
private I InternalGetDetail <I, T>(object key) where T : class, I, new() { if (String.IsNullOrEmpty(ConnectionString)) { ConnectionString = GlobalSqlContext.GetConStr(typeof(T), ""); } if (key == null) { return(default(I)); } using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString)) { con.Open(); var selectCommand = new MSSQL.MSSSqlCommandGenerater().GenerateGetItem <T>(key); selectCommand.Connection = con; var adapter = new System.Data.SqlClient.SqlDataAdapter(selectCommand as SqlCommand); DataTable table = new DataTable(); adapter.Fill(table); var Type = typeof(T); var Properties = Type.GetProperties(); if (table.Rows.Count > 0) { var item = new T(); SetObject(item, table.Rows[0], Properties, true); return(item); } return(default(I)); } }
public void Attatch <T>(T ob, OperationType SubmitType) { var gernerater = new MSSQL.MSSSqlCommandGenerater(); switch (SubmitType) { case OperationType.Create: this.TransmitCommands.Enqueue(new SqlTransAutoMic() { command = gernerater.GenerateCreateSql <T>(ob, this) as SqlCommand, ob = ob, Operation = SqlOperation.insert }); var UpdateSqlCommand = gernerater.GenerateSeletedAutoGernerateSql <T>(ob); if (UpdateSqlCommand != null) { this.TransmitCommands.Enqueue(new SqlTransAutoMic() { ob = ob, command = UpdateSqlCommand, Operation = SqlOperation.updateafterinsert }); } break; case OperationType.Save: this.TransmitCommands.Enqueue(new SqlTransAutoMic() { command = gernerater.GenerateSaveSql <T>(ob) as SqlCommand, ob = ob, Operation = SqlOperation.update }); break; case OperationType.Delete: this.TransmitCommands.Enqueue(new SqlTransAutoMic() { command = gernerater.GenerateDeleteSql <T>(ob) as SqlCommand, ob = ob, Operation = SqlOperation.delete }); break; case OperationType.DynamicUpdate: this.TransmitCommands.Enqueue(new SqlTransAutoMic() { command = gernerater.GenerateDynamicUpateSql <T>(ob), ob = ob, Operation = SqlOperation.updateafterupdate });; break; default: break; } }