Ejemplo n.º 1
0
        public int WriteChanges(string sqlStr, SqlParameter[] pams = null)
        {
            int rowsChanged = 0;

            using (var conn = SqlConnectionFactory.Create(connType))
            {
                conn.Open();
                var cmd = conn.CreateCommand();
                cmd.CommandText = sqlStr;
                if (pams != null)
                {
                    cmd.Parameters.AddRange(pams);
                }
                rowsChanged = cmd.ExecuteNonQuery();
            }
            return(rowsChanged);
        }
Ejemplo n.º 2
0
 public T ExecuteReader <T>(string sqlStr, SqlParameter[] pams = null)
 {
     using (var conn = SqlConnectionFactory.Create(connType))
     {
         conn.Open();
         var cmd = conn.CreateCommand();
         cmd.CommandText = sqlStr;
         if (pams != null)
         {
             cmd.Parameters.AddRange(pams);
         }
         object obj = cmd.ExecuteReader();
         if (obj == null || obj == DBNull.Value)
         {
             return(default(T));
         }
         return((T)Convert.ChangeType(obj, typeof(T)));
     }
 }
Ejemplo n.º 3
0
 public void InsertBulk(DataTable source, string destinationTbName, List <SqlBulkCopyColumnMapping> mappings = null)
 {
     using (var conn = SqlConnectionFactory.Create(connType))
     {
         using (var bulkCopy = new SqlBulkCopy(conn))
         {
             bulkCopy.DestinationTableName = destinationTbName;
             if (mappings == null)
             {
                 foreach (DataColumn col in source.Columns)
                 {
                     bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
                 }
             }
             else
             {
                 mappings.ForEach(m => bulkCopy.ColumnMappings.Add(m));
             }
             conn.Open();
             bulkCopy.WriteToServer(source);
         }
     }
 }