如何在asp.net中执行多个sql命令

use*_*404 1 c# sql asp.net

我有:

 string commandText = @"SELECT cn.companyName from Companies cn 
               INNER JOIN KeyProcesses uc ON uc.companyId = cn.companyId  
               WHERE uc.description like '%" + ProcessInputClause + "%';";

 string addr = @"SELECT address FROM Companies where companyName = @companyName";
Run Code Online (Sandbox Code Playgroud)

要执行我尝试过:

SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = commandText;
sqlCmd.Parameters.Clear();
string connectionString = ConfigurationSettings.AppSettings["connectionString"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
   SqlCommand command = new SqlCommand(commandText, connection);
   try
   {
       connection.Open();
       sqlCmd = new SqlCommand(sqlCmd.CommandText, connection);
       SqlDataReader sqlReader = sqlCmd.ExecuteReader();
       DataTable dt = new DataTable();

       sqlReader.Read();
       dt.Load(sqlReader);
       Label1.Text = dt.Rows[0][0].ToString();
       sqlCmd.CommandText = addr;
       SqlCmd.Parameters.AddWithValue("@companyName", "namehere");
       SqlDataReader addressReader = sqlCmd.ExecuteReader();
       addressReader.Read();
       Label1.Text = Label1.Text + addressReader["address"].ToString() + addressReader.GetValue(1).ToString() + addressReader.GetString(0) + addressReader.GetString(1);
Run Code Online (Sandbox Code Playgroud)

我只能获得第一个要执行的sql,并将companyName获取为Label1.text.但看起来第二个executeReade()没有给我任何结果,虽然我单独尝试了查询!我也尝试了combine命令文本并使用nextresult但没有运气.你能帮我解决一下如何成功执行这两个命令吗?Ps:添加了调用addressReader.Read(); 我忘了复制这个,试过这个但没有结果!

Mar*_*ell 6

首先要做的是参数化第一个查询; p

在那之后,你需要改变的是实际消耗第二个读者:

using(var addressReader = sqlCmd.ExecuteReader()) {
    if(addressReader.Read()) {
        Label1.Text = Label1.Text + addressReader["address"].ToString()
         + addressReader.GetValue(1).ToString() + addressReader.GetString(0)
         + addressReader.GetString(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:删除它,因为它不适用,因为这两个查询是分层的

可以在单个sql操作(NextResult()等)中执行两个选择,但我不确定它dt.Load是否能够正常运行; 不过值得一试:

sqlCommand.CommandText = @"
    SELECT cn.companyName from Companies cn 
    INNER JOIN KeyProcesses uc ON uc.companyId = cn.companyId  
    WHERE uc.description like '%' + @description+ '%';

    SELECT address FROM Companies where companyName = @companyName;";

sqlCommand.Parameters.AddWithValue("description", ProcessInputClause);
sqlCommand.Parameters.AddWithValue("companyName", "namehere");
using(var reader = sqlCommand.ExecuteReader()) {
    dt.Load(reader);
    if(reader.NextResult() && reader.Read()) {
        Label1.Text = Label1.Text + reader["address"].ToString()
         + reader.GetValue(1).ToString() + reader.GetString(0)
         + reader.GetString(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

由于查询之间存在依赖关系,并且地址是公司记录的一部分,我只会这样做:

@"SELECT cn.companyName, cn.Address from Companies cn 
           INNER JOIN KeyProcesses uc ON uc.companyId = cn.companyId  
           WHERE uc.description like '%" + @description + "%';";
Run Code Online (Sandbox Code Playgroud)

获取两个值的单个查询.您可以使用现有dt.Load的访问权限,从第一列获取名称,从第二列获取地址 - 但坦率地说,我是"精力充沛"的忠实粉丝,所以我这样做:

class Company {
    public string CompanyName {get;set;}
    public string Address {get;set;}
}
...
var rows = conn.Query<Company>(
    @"SELECT cn.companyName, cn.Address from Companies cn 
      INNER JOIN KeyProcesses uc ON uc.companyId = cn.companyId  
      WHERE uc.description like '%" + @description + "%';",
    new { description = ProcessInputClause }).ToList();
Run Code Online (Sandbox Code Playgroud)

然后迭代rows:

foreach(var row in rows) {
    string name = row.CompanyName;
    string address = row.Address;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想声明类型:

var rows = conn.Query(
    @"SELECT cn.companyName, cn.Address from Companies cn 
      INNER JOIN KeyProcesses uc ON uc.companyId = cn.companyId  
      WHERE uc.description like '%" + @description + "%';",
    new { description = ProcessInputClause }).ToList();
foreach(var row in rows) {
    string name = row.companyName; // yes, this works
    string address = row.Address;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

在涉及多个表的场景中,您可以使用表变量作为连接的基础:

declare @ids table (CompanyId int not null)
insert @ids (CompanyId)
select companyId from Companies cn
INNER JOIN KeyProcesses uc ON uc.companyId = cn.companyId  
WHERE uc.description like '%" + @description + "%';

select cn.CompanyName, cn.Address
from @ids #i
inner join Companies cn on cn.CompanyId = #i.CompanyId

select /* other stuff */
from @ids #i
inner join /* other tables */

select /* yet more other stuff */
from @ids #i
inner join /* yet more other tables */
Run Code Online (Sandbox Code Playgroud)