Using temporary table in c#

旧时模样 提交于 2019-12-05 05:21:15
user32957

Change your temp table from #table to ##table in both places.

Using ## means a global temp table that stays around. You'll need to Drop it after you have completed your task.

Command = " Drop Table ##table"

Putting the data into a database will take time - since you already have it in memory, perhaps LINQ-to-Objects (with DataSetExtensions) is your friend? Replace <int> etc with the correct types...

        var query = from row in table.Rows.Cast<DataRow>()
                  group row by new
                  {
                      Col1 = row.Field<int>(1),
                      Col2 = row.Field<int>(2)
                  } into grp
                  select new
                  {
                      Col1 = grp.Key.Col1,
                      Col2 = grp.Key.Col2,
                      SumCol7 = grp.Sum(x => x.Field<int>(7))
                  };
        foreach (var item in query)
        {
            Console.WriteLine("{0},{1}: {2}",
                item.Col1, item.Col2, item.SumCol7);
        }

I don't think you can make a temp table in SQL the way you are thinking, since it only exists within the scope of the query/stored procedure that creates it.

If the spreadsheet is a standard format - meaning you know the columns and they are always the same, you would want to create a Table in SQL to put this file into. There is a very fast way to do this called SqlBulkCopy

// Load the reports in bulk
SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString);
// Map the columns
foreach(DataColumn col in dataTable.Columns)
   bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
bulkCopy.DestinationTableName = "SQLTempTable";
bulkCopy.WriteToServer(dataTable);

But, if I'm understanding your problem correctly, you don't need to use SQL server to modify the data in the DataTable. You c an use the JET engine to grab the data for you.

    // For CSV
    connStr = string.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source={0};Extended Properties='Text;HDR=Yes;FMT=Delimited;IMEX=1'", Folder);
    cmdStr = string.Format("SELECT * FROM [{0}]", FileName);
    // For XLS
    connStr = string.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source={0}{1};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", Folder, FileName);
    cmdStr = "select * from [Sheet1$]";
OleDbConnection oConn = new OleDbConnection(connStr);
            OleDbCommand cmd = new OleDbCommand(cmdStr, oConn);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            oConn.Open();
            da.Fill(dataTable);
            oConn.Close();

Also, in your code you ask if your connection string is correct. I don't think it is (but I could be wrong). If yours isn't working try this.

connectionString="Data Source=localhost\<instance>;database=<yourDataBase>;Integrated Security=SSPI" providerName="System.Data.SqlClient"

Pardon me, if I have not understood what you exactly want.
If you want to perform SQL query on excel sheet, you could do it directly.

Alternatively, you can use SQL Server to query excel (OPENROWSET or a function which I dont remember right away). Using this, you can join a sql server table with excel sheet

Marc's suggestion is one more way to look at it.

Perhaps you could use a DataView. You create that from a DataTable, which you already have.

dv = new DataView(dataTableName);

Then, you can filter (apply a SQL WHERE clause) or sort the data using the DataView's methods. You can also use Find to find a matching row, or FindRows to find all matching rows.

Some filters:

dv.RowFilter = "Country = 'USA'";
dv.RowFilter = "EmployeeID >5 AND Birthdate < #1/31/82#"
dv.RowFilter = "Description LIKE '*product*'"
dv.RowFilter = "employeeID IN (2,4,5)"

Sorting:

dv.Sort = "City"

Finding a row: Find the customer named "John Smith".

   vals(0)= "John"
   vals(1) = "Smith"
   i = dv.Find(vals)

where i is the index of the row containing the customer.

Once you've applied these to the DataView, you can bind your grid to the DataView.

Change the command text from

Select col1,col2,SUM(col7) From #table group by col1,col2

to

Select col1,col2,SUM(col7) From @#table group by col1,col2
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!