public static DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query) { if (query == null) { throw new ArgumentNullException("query"); } IDbCommand cmd = ctx.GetCommand(query as IQueryable); System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(); adapter.SelectCommand = (System.Data.SqlClient.SqlCommand)cmd; DataTable dt = new DataTable(); try { if (cmd.Connection.State == ConnectionState.Closed) { cmd.Connection.Open(); } adapter.FillSchema(dt, SchemaType.Source); adapter.Fill(dt); } finally { cmd.Connection.Close(); } return(dt); }
public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query) { if (query == null) { throw new ArgumentNullException("query"); } IDbCommand cmd = ctx.GetCommand((IQueryable)query); System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(); adapter.SelectCommand = (System.Data.SqlClient.SqlCommand)cmd; DataTable dt = new DataTable("dataTbl"); cmd.Connection.ConnectionString = DynamicConnection.connectstr; try { cmd.Connection.Open(); adapter.FillSchema(dt, SchemaType.Source); adapter.Fill(dt); } finally { adapter.FillSchema(dt, SchemaType.Source); adapter.Fill(dt); cmd.Connection.Close(); } return(dt); }
public static int GetCountFromCache <T>(this System.Linq.IQueryable <T> q, System.Data.Linq.DataContext dc) { try { string CacheId = q.ToString() + "Count?"; foreach (System.Data.Common.DbParameter dbp in dc.GetCommand(q).Parameters) { CacheId += dbp.ParameterName + "=" + dbp.Value.ToString() + "&"; } object count; //if (Environment.StackTrace.Contains("CMS\\")) //{ count = q.Count(); return((int)count); //} //else //{ //System.Threading.Thread.Sleep(500); count = System.Web.HttpRuntime.Cache.Get(CacheId); // } if (count == null) { List <string> tablesNames = dc.Mapping.GetTables().Where(t => CacheId.Contains("[" + t.TableName.Substring(4) + "]")).Select(t => t.TableName.Substring(4)).ToList(); string connStr = dc.Connection.ConnectionString; using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr)) { conn.Open(); System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr); System.Web.Caching.SqlCacheDependency sqldep; AggregateCacheDependency aggDep = new AggregateCacheDependency(); foreach (string tableName in tablesNames) { if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(tableName)) { System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, tableName); } if (tableName.Contains("Comment") || tableName.Contains("PollDetail")) { sqldep = new System.Web.Caching.SqlCacheDependency("PlatformCacheSmallPollTime", tableName); } else { sqldep = new System.Web.Caching.SqlCacheDependency("PlatformCache", tableName); } aggDep.Add(sqldep); } count = q.Count(); System.Web.HttpRuntime.Cache.Insert(CacheId, count, aggDep, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration); } } //Return the created (or cached) List return((int)count); } catch (System.Exception ex) { throw ex; } }
/// <summary> /// Gets the data command for this query. /// </summary> /// <param name="dataContext">The data context to get the command from.</param> /// <returns>The requested command object.</returns> protected override DbCommand GetCommand(System.Data.Linq.DataContext dataContext) { ILinqToSqlFutureQuery futureQuery = this; var source = futureQuery.Query; // get static count method FindCountMethod(); // create count expression var genericCount = _countMethod.MakeGenericMethod(new[] { source.ElementType }); var expression = Expression.Call(null, genericCount, source.Expression); return(dataContext.GetCommand(expression)); }
public static int Delete <TEntity>(this System.Data.Linq.Table <TEntity> source, System.Linq.Expressions.Expression <Func <TEntity, bool> > predicate) where TEntity : class { System.Data.Linq.DataContext dc = source.Context; System.Data.Common.DbCommand c = dc.GetCommand(source.Where(predicate)); return(Delete(c)); }