零-点

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  /// <summary>
        /// 获取临时表-和数据库表一样的的表结构的才可以
        /// </summary>
        /// <param name="SourceTable">原数据库表</param>
        /// <param name="dt">DataTable数据集</param>
        /// <param name="column">查询列</param>
        /// <param name="where">查询条件</param>
        /// <returns>处理过的数据</returns>
        public static DataTable GetTemporaryTable(string SourceTable, DataTable dt, string column, string where)
        {
            DataTable dt2 = new DataTable();
            string tablename = "#sb" + System.DateTime.Now.ToString("MMddhhmmss");
            string sql = "select * into " + tablename + " from " + SourceTable + " where 1=0";
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand(sql, conn);
                    command.ExecuteNonQuery();
                    System.Data.SqlClient.SqlBulkCopy sq = new System.Data.SqlClient.SqlBulkCopy(conn);
                    sq.DestinationTableName = tablename;
                    sq.WriteToServer(dt);
                    if (column.IsNullOrEmpty())
                    {
                        column = "*";
                    }
                    SqlDataAdapter sd = new SqlDataAdapter("select " + column + " from " + tablename + " " + where, conn);
                    DataSet ds = new DataSet();
                    sd.Fill(ds);
                    command = new SqlCommand("drop table " + tablename, conn);
                    command.ExecuteNonQuery();
                    return ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

posted on 2016-03-25 10:55  零-点  阅读(1137)  评论(0编辑  收藏  举报