执行查询后如何在两个数据表中得到结果? [英] How can I get result in two data table after execute the query ?

查看:108
本文介绍了执行查询后如何在两个数据表中得到结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行结果后出现两个select语句。如何通过c#进入两个不同的数据表?





使用(SqlConnection con = new SqlConnection(sqlConnString))

{

使用(SqlCommand cmd = new SqlCommand())

{

cmd.CommandText =Exec SPSearchInvoiceList_rjt+ _customerID +,+ _ProjectInchargeID +,+ _ projectID +

,+ _invoiceType +,'+ _ projectTypes +',+ _invoiceAmount +,'+ _invoiceCode +

',+ _invoiceStatusID +,'+ _invoiceDateFrom +','+ _invoiceDateTo +','+

_invoiceRevisedDateFrom +','+ _invoiceRevisedDateTo +','+ _invoiceRaiseDateFrom +

','+ _invoiceRaiseDateTo +',+ Convert.ToInt32(PageIndex)+ 1 +,+ pageSize;

cmd.Connection = con;

con.Open();

var datareader = cmd.ExecuteReader();

con.Close() ;

}

}



我的尝试:



我正在尝试按上述方式执行此操作。我无法获取记录但是我能够在sql server中执行commandtext后获取数据。请帮忙。

After execution result is coming in two select statement. How can get in two different data table by c#?


using (SqlConnection con = new SqlConnection(sqlConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Exec SPSearchInvoiceList_rjt " + _customerID + "," + _ProjectInchargeID + "," + _projectID +
"," + _invoiceType + ",'" + _projectTypes + "'," + _invoiceAmount + ",'" + _invoiceCode +
"'," + _invoiceStatusID + ",'" + _invoiceDateFrom + "','" + _invoiceDateTo + "','" +
_invoiceRevisedDateFrom + "','" + _invoiceRevisedDateTo + "','" + _invoiceRaiseDateFrom +
"','" + _invoiceRaiseDateTo + "'," + Convert.ToInt32(PageIndex) + 1 + "," + pageSize;
cmd.Connection = con;
con.Open();
var datareader = cmd.ExecuteReader();
con.Close();
}
}

What I have tried:

I am trying to do it as above.I am unable to get records but i am able to get data once i execute commandtext in sql server. Please help.

推荐答案

使用 SqlDataAdapter [ ^ ]



use SqlDataAdapter[^]

cmd.Connection = con;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

DataTable dt1 = ds.Tables[0]; // first select result
DataTable dt2 = ds.Tables[1]; // second select result





注意:您的代码是易受攻击 SQL注入 [ ^ ]攻击

总是使用参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]



note: your code is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]


这篇关于执行查询后如何在两个数据表中得到结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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