示例#1
0
    public static void Update <T>(this ISqlConnectionProvider sqlConnectionProvider,
                                  T objectToUpdate,
                                  Expression <Func <T, object> > keyProperties = null,
                                  string tableName        = null,
                                  string schemaName       = null,
                                  bool processColumnNames = true)
    {
        tableName = SqlTextFunctions.GetTableName(objectToUpdate.GetType(), tableName, schemaName);

        using (var myConnection = sqlConnectionProvider.ToSqlConnection())
        {
            using (var myCommand = new SqlCommand {
                Connection = myConnection
            })
            {
                var setClauseProperties   = SqlClrHelpers.GetRelevantPropertyInfos(objectToUpdate, null);
                var whereClauseProperties = SqlClrHelpers.GetPropertiesFromObject(objectToUpdate, keyProperties);
                SqlTextFunctions.UnUnderscoreColumnNames = processColumnNames;
                BuildOutUpdateCommand(objectToUpdate,
                                      tableName,
                                      setClauseProperties,
                                      whereClauseProperties,
                                      myCommand,
                                      processColumnNames);
                SqlTextFunctions.UnUnderscoreColumnNames = true;
                myConnection.Open();
                SqlLogger.LogSqlCommand(myCommand);
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }
    }
    public static void Insert(this ISqlConnectionProvider sqlConnectionProvider,
                              object obj,
                              string tableName  = null,
                              string schemaName = null,
                              IEnumerable <string> columnsToIgnore = null,
                              bool processColumnNames = true,
                              bool useIdentityInsert  = false)
    {
        tableName = SqlTextFunctions.GetTableName(obj.GetType(), tableName, schemaName);

        var propertyInfos = SqlClrHelpers.GetRelevantPropertyInfos(obj, columnsToIgnore);

        if (propertyInfos.Count == 0)
        {
            return;
        }

        using (var myConnection = sqlConnectionProvider.GetSqlConnectionWithTimeout(60))
        {
            using (var myCommand = new SqlCommand {
                Connection = myConnection
            })
            {
                SqlTextFunctions.UnUnderscoreColumnNames = processColumnNames;
                BuildOutMyCommand(obj, tableName, propertyInfos, myCommand, processColumnNames);
                SqlTextFunctions.UnUnderscoreColumnNames = true;

                myConnection.Open();
                SqlLogger.LogSqlCommand(myCommand);

                // if (useIdentityInsert)
                //     myConnection.Execute("set identity_insert on;");
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }
    }