使用SQL Server 2005,我试图从一个表中选择一定数量的记录(动态),基于另一个表来获取需要的记录数.
表1有一个类别ID和我想要为该类别返回的记录数.
Category ID TOP_Limit
----------------------
Cat 1 1
Cat 2 2
Cat 3 10
Run Code Online (Sandbox Code Playgroud)
表2有产品ID,类别ID和数量:
Product ID Category ID Quantity
---------------------------------
Part 1 Cat 1 10
Part 2 Cat 1 20
Part 3 Cat 2 100
Part 4 Cat 2 100
Part 5 Cat 2 50
Part 6 Cat 3 5
Run Code Online (Sandbox Code Playgroud)
如何编写一个查询,从表2(第2部分,第3部分和第4部分,第6部分)获得正确的"顶级"产品记录?
我目前有一个处理程序,它接受excel文件的文件路径和tabname,将文件处理成数据表,然后将表序列化为json字符串以返回.这是有效的,直到我尝试处理大文件,然后我得到一个内存不足异常.
我想如果我没有先将所有内容加载到数据表中,而是直接加载到json字符串中,它会减少内存使用量.但是,我一直无法找到如何执行此操作的任何示例.
我可以直接从OleDbConnection序列化为字符串吗?怎么样?
public void ProcessRequest(HttpContext context)
{
string path = context.Request["path"];
string tableNames = context.Request["tableNames"];
string connectionString = string.Empty;
if (path.EndsWith(".xls"))
{
connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source={0};
Extended Properties=""Excel 8.0;HDR=YES;IMEX=1""", path);
}
else if (path.EndsWith(".xlsx"))
{
connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source={0};
Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1""", path);
}
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbDataAdapter adapter = factory.CreateDataAdapter();
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
DataTable tmp = new DataTable();
DbCommand selectCommand = factory.CreateCommand();
selectCommand.CommandText = String.Format("SELECT * FROM [{0}]", tableNames);
selectCommand.Connection = …Run Code Online (Sandbox Code Playgroud)