在C#中怎样把sql查询的结果存放到datatable里?困扰我好久了,希望高手能把代码写出来 万分感谢
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在C#中怎样把sql查询的结果存放到datatable里?困扰我好久了,希望高手能把代码写出来 万分感谢相关的知识,希望对你有一定的参考价值。
for(int i=0;i<dt.rows.count;i++)
ye[i] = dt.Rows[i]["SQYE"].ToString()
类似这种形式的
调用这个函数时
建一个SqlDataAdapter
SqlCommand comm = new SqlCommand("select * from table where ...");
SqlDataAdapter ad = new SqlDataAdapter(comm);
public int MyExecuteSql(SqlDataAdapter sqlAd, ref DataTable returnTable)
try
this.openConn();
sqlAd.SelectCommand.Connection = this.conn;
DataSet ds = new DataSet();
sqlAd.Fill(ds);
returnTable = ds.Tables[0];
if (ds.Tables[0].Rows.Count > 0)
return 1;
else
return 0;
catch (Exception ex)
throw ex;
finally
this.closeConn();
//return 0;
希望能有帮助 参考技术A 先引用System.Data;System.Data.SqlClient。
代码如下:
string strConn = "Server=dbIP; database=dbname; uid=id; pwd=pwd;";
string strSql = "select score from student";
SqlDataAdapter da = new SqlDataAdapter(strSql, strConn);
DataTable dt = new DataTable();
da.Fill(dt); //获取到的内容填充到DataTable中 参考技术B 首先引入datatable所在命名空间 using System.Data;
DataSet ds = new DataSet();
using (OracleConnection connection = new OracleConnection(connectionString))
//create a command and prepare it for execution
using (OracleCommand cmd = new OracleCommand())
try
//prepare command for execution
PrepareCommand(cmd, connection, null, sqlText, cmdParms);
//create the DataAdapter
OracleDataAdapter da = new OracleDataAdapter(cmd);
//fill the DataSet using default values for DataTable names, etc.
da.Fill(ds, "ds");
// detach the OracleParameters from the command object, so they can be used again.
cmd.Parameters.Clear();
return ds.table[0];最后一句返回了datatable. 参考技术C OracleConnection thisConnection = new OracleConnection(@"Data Source=orcl;User ID=shibei;Password=nsic;Unicode=True");
// thisConnection.Open();//此处可以不用打开
OracleDataAdapter thisAdapter = new OracleDataAdapter("select * from N_AIT", thisConnection);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "MY_N_AIT");//MY_N_AIT不是表名而是DataTable对象的名称
DataTable dt = thisDataSet .Tables[0]; 参考技术D 直接用SqlDataAdapter填充DataTable也可以的
怎样从生产数据库中获得想要的查询语句,把结果集批量插入到磁盘txt文件中
第一步:建立存储过程实现传入参数,生成结果到磁盘的txt文件中;
USE [XXXXX]
GO
/****** Object: StoredProcedure [dbo].[Proc_sql_to_file] Script Date: 2018/5/13 15:47:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,华为厂商为了获取本系统数据写此存储过程,调用此过程生成txt文件保存到服务器,然后ftp到华为,>
-- =============================================
ALTER PROCEDURE [dbo].[Proc_sql_to_file](
@servername varchar(50),--服务器名
@username varchar(50),--用户名,如果用NT验证方式,则为空''
@password varchar(50) ,--密码
@sql_query varchar(2000) , --SQL查询语句,有字符条件时用单引号
@filePath_name varchar(1000), --导出路径和文件名
@date datetime --保存文件时的生成日期戳
)AS
DECLARE @sql varchar(8000)
SET @sql='bcp "'[email protected]_query + '" queryout '+' "'[email protected]_name+'_'+
convert(varchar(10),@date,21)+'.txt " /t "," /w'
+' /S '[email protected]
+CASE WHEN ISNULL(@username,'')='' THEN '' ELSE ' /U '[email protected] END
+' /P '+ISNULL(@password,'')
select @sql
EXEC master..xp_cmdshell @sql
IF @@Error<>0 BEGIN
print '生成文件时出错了,请查看!'[email protected]@Error
END
第二步:数据库中新建任务计划;
use XXXXX;---数据库名称
go
if exists(select 1 from sysobjects where name='temp11')
drop table temp11;
if exists(select 1 from sysobjects where name='temp12')
drop table temp12;
go
go
declare @date1 datetime;set @date1 =CONVERT(varchar(10),Getdate(), 21);
--华为采购数量时间为当天;
select CONVERT(varchar(100), GETDATE(), 102)+'入库串码' 手机串码,'数量' 数量,'仓库' 仓库 into temp11
union all
select
' '+t4.FSERIALNO,convert(varchar,1) Fqty,tl.fname FstockName
from T_STK_INSTOCK t1
inner join T_STK_INSTOCKentry t2 on t1.fid=t2.fid
inner join T_STK_INSTOCKENTRY_F t3 on t2.fid=t3.fid
left join T_STK_INSTOCKSERIAL t4 on t3.FENTRYID=t4.FENTRYID
inner join t_bd_material_l t6 on t2.FMATERIALID=t6.FMATERIALID and t6.FLOCALEID=2052
left join T_BD_STOCK_L tl on t2.FSTOCKID=tl.FSTOCKID and tl.FLOCALEID=2052
inner join ( select distinct m.FMATERIALID,m.FCATEGORYID,w.fname from T_BD_MATERIALBASE m
join T_BD_MATERIALCATEGORY_L w on m.FCATEGORYID=w.FCATEGORYID
) u on t6.FMATERIALID=u.FMATERIALID
where u.fname like '%手机%' and t6.fname like '%华为%'
and convert(varchar(10),t1.FDATE,21)=convert(varchar(10),getdate(),21)
--库存数量
select CONVERT(varchar(100), GETDATE(), 102)+'在库串码' 手机串码,'数量' 数量,'仓库' 仓库 into temp12
union all
SELECT FNUMBER,convert(varchar,1) Fqty,ckl.FNAME FstockName
FROM (select FNUMBER,FSERIALID,FMATERIALID from V_BD_OVERASERIALVIEW where FSERIALID>1 and FDOCUMENTSTATUS = 'C' AND FFORBIDSTATUS = 'A') t0
INNER JOIN (select FSERIALID from V_BD_OVERASERIALVIEW_L where FSERIALID>1 AND FLocaleId = 2052) t0_L ON (t0.FSERIALID = t0_L.FSERIALID)
INNER JOIN (select FSERIALID From V_BD_OVERASERIALORGVIEW where FSERIALID>1 and FSTOCKSTATUS = 1 ) uu on uu.FSERIALID=t0.FSERIALID
inner join (select FMATERIALID,FNAME FSPMC From t_bd_material_l where FMATERIALID>1 and FLocaleId = 2052) tll on t0.FMATERIALID =tll.FMATERIALID
INNER join (select FCATEGORYID,FMATERIALID From T_BD_MATERIALBASE where FCATEGORYID>1 and FCATEGORYID ='2092334' ) m on t0.FMATERIALID=m.FMATERIALID
left join (SELECT FSERIALID,MIN(FINPUTDATE) FINPUTDATE FROM (select a.FSERIALID FSERIALID,case when b.FINPUTDATE not like '' then b.FINPUTDATE else a.FBILLDATE
end FINPUTDATE from (select FSERIALID,FBILLDATE From T_BD_SERIALBILLTRACE_e where FSERIALID>1 ) a INNER join (select FSERIALID,FINPUTDATE From T_BD_SERIALMASTEROTHER where FSERIALID>1) b on a.FSERIALID=b.FSERIALID ) a GROUP BY FSERIALID) b on t0.fserialid=b.FSERIALID
INNER JOIN (select FSERIALID,FBILLTRACEID,FINVID from T_BD_SERIALBILLTRACE where FBILLTRACEID>1 and FSERIALID>1 ) t5 ON T0.FSERIALID = t5.FSERIALID AND t5.FBILLTRACEID = (SELECT top 1 EE.FBILLTRACEID AS FBILLTRACEID FROM T_BD_SERIALBILLTRACE EE WHERE T0.FSERIALID = EE.FSERIALID and( FINVID is not null and FINVID <> '' ) order by FBILLTRACEID desc)
inner join (select fid,FSTOCKID,FSTOCKSTATUSID from T_STK_INVENTORY where FSTOCKSTATUSID=10000 and FOBJECTTYPEID= 'STK_Inventory' and FKEEPERTYPEID='BD_KeeperOrg'and FOWNERTYPEID='BD_OwnerOrg' and FISEFFECTIVED=1 and FBASEQTY<>0) inv on inv.fid=t5.FINVID
inner join (select FSTOCKID,FNAME from T_BD_STOCK_L where FSTOCKID>1 and FLocaleId = 2052 ) ckl on inv.FSTOCKID=ckl.FSTOCKID
where FSPMC like '%华为%'
option(maxdop 0)
EXEC XXXXX..Proc_sql_to_file 'DESKTOP-89KSR2K','sa','dingweitao','select * from XXXXX.dbo.temp11','d:\k3cloud_Instock\k3cloud_Instock',@date1
EXEC XXXXX..Proc_sql_to_file 'DESKTOP-89KSR2K','sa','dingweitao','select * from XXXXX.dbo.temp12','d:\k3cloud_Inventory\k3cloud_Inventory',@date1
go
drop table temp11,temp12
go
最终每天会更新数据到文件中;
以上是关于在C#中怎样把sql查询的结果存放到datatable里?困扰我好久了,希望高手能把代码写出来 万分感谢的主要内容,如果未能解决你的问题,请参考以下文章