在C#中使用临时表 [英] Using temporary table in c#

查看:829
本文介绍了在C#中使用临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一个Excel工作表到datagrid.From那里,我已成功地阅读网格的行成一个DataTable object.The DataTable对象有数据,因为当我做等于一个网格的数据源的表对象,网格填充



我的问题:我想使用的表对象,并使用SQL服务器操纵它的值(即我想将它保存为一个临时表,并使用SQL操纵它在C#代码查询和,我希望它返回不同的结果INTE的网格。(我不知道如何使用临时表在C#中工作)



下面是代码点击按钮....

  SqlConnection的康恩=新的SqlConnection(在执行服务器=本地主机;集成安全= SSPI ); 
//是连接字符串不正确

的SqlCommand CMD =新的SqlCommand();

// !!方法ConvertFPSheetDataTable返回一个DataTable对象//
cmd.Parameters.AddWithValue(#表,ConvertFPSheetDataTable(12,假的,fpSpread2_Sheet1));
//我想创建临时表

//这里,我做一个查询
cmd.CommandText =选择COL1,COL2,SUM(COL7)从#table GROUP BY COL1,COL2降#table

SqlDataAdapter的大=新SqlDataAdapter的(cmd.CommandText,康涅狄格州);
DataTable的DT =新的DataTable();
da.Fill(DT); *** //我得到一个错误在这里无效的对象名称#table ***

fpDataSet_Sheet1.DataSource DT ='';

// **注:** fpDataSet_Sheet1是网格控制


解决方案

这#table更改临时表,以##台在这两个地方。



使用##意味着保持一个全局临时表周围。你将需要删除它,你已经完成了你的任务后。



命令=删除表##表


I read an excel sheet into a datagrid.From there , I have managed to read the grid's rows into a DataTable object.The DataTable object has data because when I make equal a grid's datasource to that table object , the grid is populated.

My Problem : I want to use the table object and manipulate its values using SQL server,(i.e. I want to store it as a temporary table and manipulate it using SQL queries from within C# code and , I want it to return a different result inte a grid.(I don't know how to work with temporary tables in C#)

Here's code to execute when clicking button....

 SqlConnection conn = new SqlConnection("server = localhost;integrated security = SSPI");
//is connection string incorrect?

SqlCommand cmd = new SqlCommand();

//!!The method ConvertFPSheetDataTable Returns a DataTable object//
cmd.Parameters.AddWithValue("#table",ConvertFPSheetDataTable(12,false,fpSpread2_Sheet1));
//I am trying to create temporary table     

//Here , I do a query               
cmd.CommandText = "Select col1,col2,SUM(col7) From #table group by col1,col2 Drop #table";

SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText,conn);
 DataTable dt = new DataTable();
da.Fill(dt); ***// I get an error here 'Invalid object name '#table'.'***

fpDataSet_Sheet1.DataSource = dt;

//**NOTE:** fpDataSet_Sheet1 is the grid control

解决方案

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"

这篇关于在C#中使用临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆