1

我试图从数据库中的几个表中获取一系列值。

我目前有这个代码工作:

SqlDataReader reader = null;
SqlCommand myCommand = new SqlCommand("select * from ods_LN_Master", xavierConnection);

reader = myCommand.ExecuteReader();
while (reader.Read())
{
   Console.WriteLine(reader["LMBKNO"].ToString());
}

然而,我真正想做的 SQL 查询是这样的:

select "T1"."C0" AS "Collateral Type", "T1"."C1" AS "Owner Occuppied", "T1"."C2" AS "County Name", "T1"."C3" AS "Region Description", "T1"."C4" AS "City", "T1"."C5" AS "Orig Appraisal Date", "T1"."C6" AS "Curr Appraisal Date", "T1"."C7" AS "Mortgage 1 Bank", "T1"."C8" AS "Mortgage 2 Bank", "T1"."C9" AS "Bank Owned Mortgage 1", "T1"."C10" AS "Bank Owned Mortgage 2", "T1"."C11" AS "Call Report Code", "T1"."C12" AS "Original Note Date", "T1"."C13" AS "Account Key", "T1"."C14" AS "Account Number", "T1"."C15" AS "Credit Risk Code", "T1"."C16" AS "Current FICO Score", "T1"."C17" AS "Original FICO Score", "T0"."C18" AS "Bank Owned Principal Assets", "T0"."C19" AS "Orig Appraisal Amount", "T0"."C20" AS "Curr Appraisal Amount", "T0"."C21" AS "Mortgage Amount 1", "T0"."C22" AS "Mortgage Amount 2", "T1"."C18" AS "Available Balance", "T1"."C19" AS "Past Due Counter 1", "T1"."C20" AS "Past Due Counter 2", "T1"."C21" AS "Past Due Counter 3", "T1"."C22" AS "Past Due Counter 4", "T1"."C23" AS "Past Due Counter 5", "T1"."C24" AS "Past Due Counter 6"
 from (
select "CT_Master"."Collateral Type" AS "C0", "CTPROP"."Owner Occuppied" AS "C1", "CTPROP"."County Name" AS "C2", "Org"."Region Description" AS "C3", "CTPROP"."City" AS "C4", "CTPROP"."Orig Appraisal Date" AS "C5", "CTPROP"."Curr Appraisal Date" AS "C6", "CTPROP"."Mortgage 1 Bank" AS "C7", "CTPROP"."Mortgage 2 Bank" AS "C8", "CTPROP"."Bank Owned Mortgage 1" AS "C9", "CTPROP"."Bank Owned Mortgage 2" AS "C10", "LN_Master"."Call Report Code" AS "C11", case  when isdate("LN_Master"."Original Note Date") = 1 then convert(datetime,convert(varchar,"LN_Master"."Original Note Date")) else NULL end  AS "C12", "LN_Master"."Account Key" AS "C13", "LN_Master"."Account Number" AS "C14", "LN_Master"."Credit Risk Code" AS "C15", "SIFINA"."10 Char Spare Code 1" AS "C16", "SIFINA"."10 Char Spare Code 2" AS "C17", sum("LNIMPR"."Book_Balance" - "Participation_Lookups"."Participation Principal Assets") AS "C18", sum("CTPROP"."Orig Appraisal Amount") AS "C19", sum("CTPROP"."Curr Appraisal Amount") AS "C20", sum("CTPROP"."Mortgage Amount 1") AS "C21", sum("CTPROP"."Mortgage Amount 2") AS "C22"
 from (((((((
select "ods_LN_Master"."Account Count",  "ods_LN_Master"."Current Loan Status Code", "ods_LN_Master"."Account Key", "ods_LN_Master"."Org SKey", "ods_LN_Master"."Impaired Asset Status", "ods_LN_Master"."Credit Risk Code", "ods_LN_Master"."Call Report Code",  "ods_LN_Master"."Original Note Date", "ods_LN_Master"."Bank Number", "ods_LN_Master"."Account Number", "ods_LN_Master"."Participation Number", "ods_LN_Master"."Customer Number", "ods_LN_Master"."Dealer Number", "ods_LN_Master"."CIF Key", "ods_LN_Master"."Short Name", "ods_LN_Master"."Converted Account Key", "ods_LN_Master"."Branch Number", "ods_LN_Master"."Proc Application Code",  "ods_LN_Master"."Product Code",  "ods_LN_Master"."Account Application Code"
 from "BANK38"."dbo"."v_ods_LN_Master" "ods_LN_Master"
 where ods_LN_Master."Participation Number" = 0) "LN_Master" INNER JOIN (
select "Account Key", "Participation Principal Assets"
 from "BANK38"."dbo"."v_ods_Fact_AllProds_2008"
 where "Application Code" = 'LN') "Participation_Lookups" on "LN_Master"."Account Key" = "Participation_Lookups"."Account Key") LEFT OUTER JOIN (
select "ods_LNIMPR"."Account Key" AS "Account_Key", "ods_LNIMPR"."Book Balance" AS "Book_Balance"
 from "BANK38"."dbo"."v_ods_LNIMPR" "ods_LNIMPR"
 where ods_LNIMPR."Participation Number" = 0) "LNIMPR" on "LN_Master"."Account Key" = "LNIMPR"."Account_Key") LEFT OUTER JOIN "BANK38"."dbo"."fi_Core_Org" "Org" on "LN_Master"."Org SKey" = "Org"."Org Key") LEFT OUTER JOIN (
select *
 from "BANK38"."dbo"."v_ods_SICOD1" "ods_CODE_Master"
 where "ods_CODE_Master"."Application" = 'LN' and "ods_CODE_Master"."Code Type" = 'STAT') "Status_Code" on "LN_Master"."Bank Number" = "Status_Code"."Bankid" and "LN_Master"."Current Loan Status Code" = "Status_Code"."User Code") LEFT OUTER JOIN (
select "V_ODS_SIFINA"."10 Char Spare Code 1", "V_ODS_SIFINA"."10 Char Spare Code 2", "V_ODS_SIFINA"."Account Key"
 from "BANK38"."dbo"."V_ODS_SIFINA" "V_ODS_SIFINA"
 where "V_ODS_SIFINA"."APPLICATION" = 'LN') "SIFINA" on "LN_Master"."Account Key" = "SIFINA"."Account Key") LEFT OUTER JOIN ("BANK38"."dbo"."v_ods_CT_Master" "CT_Master" INNER JOIN (
select "v_ods_CTACTREL"."Account Number", "v_ods_CTACTREL"."Collateral Key"
 from "BANK38"."dbo"."v_ods_CTACTREL" "v_ods_CTACTREL"
 where v_ods_CTACTREL."Application" = 'LN') "CTACTREL" on "CT_Master"."Collateral Key" = "CTACTREL"."Collateral Key") on "LN_Master"."Account Number" = "CTACTREL"."Account Number") LEFT OUTER JOIN "BANK38"."dbo"."v_ods_CTPROP" "CTPROP" on "CT_Master"."Collateral Key" = "CTPROP"."Collateral Key"
 where case  when ("LN_Master"."Impaired Asset Status" = 'C') then 'H' else "Status_Code"."Code Key 2" end  <> 'C' and "LN_Master"."Proc Application Code" in ('RE', 'IL', 'CL') and case  when ("LN_Master"."Impaired Asset Status" = 'C') then 'H' else "Status_Code"."Code Key 2" end  <> 'H' and "CT_Master"."Collateral Type" in ('430', '431') and "CTPROP"."Owner Occuppied" in ('O')
 group by "CT_Master"."Collateral Type", "CTPROP"."Owner Occuppied", "CTPROP"."County Name", "Org"."Region Description", "CTPROP"."City", "CTPROP"."Orig Appraisal Date", "CTPROP"."Curr Appraisal Date", "CTPROP"."Mortgage 1 Bank", "CTPROP"."Mortgage 2 Bank", "CTPROP"."Bank Owned Mortgage 1", "CTPROP"."Bank Owned Mortgage 2", "LN_Master"."Call Report Code", case  when isdate("LN_Master"."Original Note Date") = 1 then convert(datetime,convert(varchar,"LN_Master"."Original Note Date")) else NULL end , "LN_Master"."Account Key", "LN_Master"."Account Number", "LN_Master"."Credit Risk Code", "SIFINA"."10 Char Spare Code 1", "SIFINA"."10 Char Spare Code 2") "T0", (
select "CT_Master"."Collateral Type" AS "C0", "CTPROP"."Owner Occuppied" AS "C1", "CTPROP"."County Name" AS "C2", "Org"."Region Description" AS "C3", "CTPROP"."City" AS "C4", "CTPROP"."Orig Appraisal Date" AS "C5", "CTPROP"."Curr Appraisal Date" AS "C6", "CTPROP"."Mortgage 1 Bank" AS "C7", "CTPROP"."Mortgage 2 Bank" AS "C8", "CTPROP"."Bank Owned Mortgage 1" AS "C9", "CTPROP"."Bank Owned Mortgage 2" AS "C10", "LN_Master"."Call Report Code" AS "C11", case  when isdate("LN_Master"."Original Note Date") = 1 then convert(datetime,convert(varchar,"LN_Master"."Original Note Date")) else NULL end  AS "C12", "LN_Master"."Account Key" AS "C13", "LN_Master"."Account Number" AS "C14", "LN_Master"."Credit Risk Code" AS "C15", "SIFINA"."10 Char Spare Code 1" AS "C16", "SIFINA"."10 Char Spare Code 2" AS "C17", "LN_Master"."Available Balance" AS "C18", "LN_Master"."Past Due Counter 1" AS "C19", "LN_Master"."Past Due Counter 2" AS "C20", "LN_Master"."Past Due Counter 3" AS "C21", "LN_Master"."Past Due Counter 4" AS "C22", "LN_Master"."Past Due Counter 5" AS "C23", "LN_Master"."Past Due Counter 6" AS "C24"
 from (((((((
select "ods_LN_Master"."Account Count", "ods_LN_Master"."Current Loan Status Code", "ods_LN_Master"."Credit Risk Code", "ods_LN_Master"."Call Report Code",  "ods_LN_Master"."Impaired Asset Status", "ods_LN_Master"."Past Due Counter 4", "ods_LN_Master"."Past Due Counter 5", "ods_LN_Master"."Past Due Counter 6", "ods_LN_Master"."Past Due Counter 1", "ods_LN_Master"."Past Due Counter 2", "ods_LN_Master"."Past Due Counter 3", "ods_LN_Master"."Available Balance",  "ods_LN_Master"."Original Note Date", "ods_LN_Master"."Org SKey", "ods_LN_Master"."Account Key", "ods_LN_Master"."Ext Application Code", "ods_LN_Master"."Bank Number", "ods_LN_Master"."Account Number", "ods_LN_Master"."Participation Number", "ods_LN_Master"."CIF Key",  "ods_LN_Master"."Proc Application Code", "ods_LN_Master"."Product Code"
 from "BANK38"."dbo"."v_ods_LN_Master" "ods_LN_Master"
 where ods_LN_Master."Participation Number" = 0) "LN_Master" INNER JOIN (
select "Account Key", "Participation Principal Assets"
 from "BANK38"."dbo"."v_ods_Fact_AllProds_2008"
 where "Application Code" = 'LN') "Participation_Lookups" on "LN_Master"."Account Key" = "Participation_Lookups"."Account Key") LEFT OUTER JOIN (
select "ods_LNIMPR"."Account Key" AS "Account_Key", "ods_LNIMPR"."Book Balance" AS "Book_Balance"
 from "BANK38"."dbo"."v_ods_LNIMPR" "ods_LNIMPR"
 where ods_LNIMPR."Participation Number" = 0) "LNIMPR" on "LN_Master"."Account Key" = "LNIMPR"."Account_Key") LEFT OUTER JOIN "BANK38"."dbo"."fi_Core_Org" "Org" on "LN_Master"."Org SKey" = "Org"."Org Key") LEFT OUTER JOIN (
select *
 from "BANK38"."dbo"."v_ods_SICOD1" "ods_CODE_Master"
 where "ods_CODE_Master"."Application" = 'LN' and "ods_CODE_Master"."Code Type" = 'STAT') "Status_Code" on "LN_Master"."Bank Number" = "Status_Code"."Bankid" and "LN_Master"."Current Loan Status Code" = "Status_Code"."User Code") LEFT OUTER JOIN (
select "V_ODS_SIFINA"."10 Char Spare Code 1", "V_ODS_SIFINA"."10 Char Spare Code 2", "V_ODS_SIFINA"."Account Key"
 from "BANK38"."dbo"."V_ODS_SIFINA" "V_ODS_SIFINA"
 where "V_ODS_SIFINA"."APPLICATION" = 'LN') "SIFINA" on "LN_Master"."Account Key" = "SIFINA"."Account Key") LEFT OUTER JOIN ("BANK38"."dbo"."v_ods_CT_Master" "CT_Master" INNER JOIN (
select "v_ods_CTACTREL"."Account Number", "v_ods_CTACTREL"."Collateral Key"
 from "BANK38"."dbo"."v_ods_CTACTREL" "v_ods_CTACTREL"
 where v_ods_CTACTREL."Application" = 'LN') "CTACTREL" on "CT_Master"."Collateral Key" = "CTACTREL"."Collateral Key") on "LN_Master"."Account Number" = "CTACTREL"."Account Number") LEFT OUTER JOIN "BANK38"."dbo"."v_ods_CTPROP" "CTPROP" on "CT_Master"."Collateral Key" = "CTPROP"."Collateral Key"
 where case  when ("LN_Master"."Impaired Asset Status" = 'C') then 'H' else "Status_Code"."Code Key 2" end  <> 'C' and "LN_Master"."Proc Application Code" in ('RE', 'IL', 'CL') and case  when ("LN_Master"."Impaired Asset Status" = 'C') then 'H' else "Status_Code"."Code Key 2" end  <> 'H' and "CT_Master"."Collateral Type" in ('430', '431') and "CTPROP"."Owner Occuppied" in ('O')) "T1"
 where ("T1"."C0" = "T0"."C0" or "T1"."C0" is null and "T0"."C0" is null) and ("T1"."C1" = "T0"."C1" or "T1"."C1" is null and "T0"."C1" is null) and ("T1"."C2" = "T0"."C2" or "T1"."C2" is null and "T0"."C2" is null) and ("T1"."C3" = "T0"."C3" or "T1"."C3" is null and "T0"."C3" is null) and ("T1"."C4" = "T0"."C4" or "T1"."C4" is null and "T0"."C4" is null) and ("T1"."C5" = "T0"."C5" or "T1"."C5" is null and "T0"."C5" is null) and ("T1"."C6" = "T0"."C6" or "T1"."C6" is null and "T0"."C6" is null) and ("T1"."C7" = "T0"."C7" or "T1"."C7" is null and "T0"."C7" is null) and ("T1"."C8" = "T0"."C8" or "T1"."C8" is null and "T0"."C8" is null) and ("T1"."C9" = "T0"."C9" or "T1"."C9" is null and "T0"."C9" is null) and ("T1"."C10" = "T0"."C10" or "T1"."C10" is null and "T0"."C10" is null) and "T1"."C11" = "T0"."C11" and ("T1"."C12" = "T0"."C12" or "T1"."C12" is null and "T0"."C12" is null) and ("T1"."C13" = "T0"."C13" or "T1"."C13" is null and "T0"."C13" is null) and "T1"."C14" = "T0"."C14" and "T1"."C15" = "T0"."C15" and ("T1"."C16" = "T0"."C16" or "T1"."C16" is null and "T0"."C16" is null) and ("T1"."C17" = "T0"."C17" or "T1"."C17" is null and "T0"."C17" is null)

它是从我公司用来查看数据库中内容的不同程序自动生成的。我知道它会返回我正在寻找的确切数据。我暂时想使用这个 SQL,因为我还没有跟上我们的数据库架构的速度(而且我不会在很长一段时间内,它的巨大....”)

我需要做什么才能将丑陋的 SQL 查询放入我的 C# 代码中,以便它的行为方式与我的简单语句相同?

我目前的问题是双引号。有没有我可以做的 Find/Peplace 让它工作?

4

1 回答 1

4

我只需将其复制并粘贴到存储过程中,然后使用ADO.NET调用存储过程

编辑:

using (var conn = new SqlConnection("<connection string>")) {
    using (var cmd = new SqlCommand("MyStoredProcedureName")) {
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            // Use the reader to read data
        }
    }
}

仅供参考,不使用存储过程的另一种方法是在项目中创建一个新的文本文件,然后将 SQL 复制并粘贴到该文本文件中。将文本文件的构建操作更改为 Embedded Resource,然后使用GetManifestResourceStream从资源中提取 SQL

所以像这样:

string sql = "";
using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().
       GetManifestResourceStream("<AssemblyName>.<NameOfTextFile>")) {
    sql = reader.ReadToEnd();
}

if (!string.IsNullOrEmpty(sql))
{
    using (var conn = new SqlConnection("<connection string>")) {
        using (var cmd = new SqlCommand(sql)) {
            cmd.CommandType = CommandType.Text;
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                // Use the reader to read data
            }
        }
    }
}
于 2013-04-04T20:00:06.090 回答