I don’t think you can make a temporary table in SQL the way you think, because it exists only within the scope of the query / stored procedure that creates it.
If the spreadsheet is the standard format, it means that you know the columns, and they are always the same, you would like to create a table in SQL to accommodate this file. There is a very quick 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 understand your problem correctly, you do not need to use a SQL server to modify data in a DataTable. You use the JET mechanism to capture 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();
In addition, in the code you ask for the correct connection string. I do not think it is (but I could be wrong). If yours does not work, try this.
connectionString="Data Source=localhost\<instance>;database=<yourDataBase>;Integrated Security=SSPI" providerName="System.Data.SqlClient"
Ryan abbott
source share