c#声明记录集变量等于存储过程中的字符串

时间:2017-07-11 13:10:19

标签: c# asp.net vb.net

我将vb代码中的前一个经典asp转换为c#中的asp.net。我无法为记录集存储过程变量(objRs)分配变量。我试过把它设置为     strap = objRs("strap"); 但这给了我一个错误的objRS"局部变量ADODB.Recordset objRS - 方法代表或事件是预期的"

我用括号尝试过:     strap = objRs["strap"]; 这给了我第二个带#34;不明确的索引器:ADODB.Fields字段(在interface_Recordset中)的错误; ADODB.Fields字段(在interface_Recordset15中); "" (在interface_Recordset20中); "" (在interface_Recordset21中);匹配"

我也尝试了各种Convert.ToString方法,但没有成功。

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.EnterpriseServices" %>
<%@ Import Namespace="System.Security.Cryptography.X509Certificates" %>
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb.OleDbConnection;  
using ADODB;
<%  

%>
<!DOCTYPE html HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"https://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=8, IE=10, IE=11" />

    <!--The viewport meta tag is used to improve the presentation and behavior on iOS devices--> 

    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <title>My Mapping Application</title>
    <script runat="server">



    private void OpenConnection(string connectionString)
    {
        string txtTaxYear;
        //txtTaxYear;
        string webacct, strap, straptype = null, ownername, facetMap;

        txtTaxYear = Request.Form["ddlTaxYear"].Trim();
        webacct = Request.Form["txtAcct"].Trim();
        nhCd = Request.Form["nh_cd"].Trim();
        nhGrpCd = Request.Form["nh_grp_cd"].Trim();

        //var objConn = null;

        OleDbConnection objConn = new OleDbConnection("Provider=SQLOLEDB; DRIVER={SQL Server};SERVER=XYZserver;UID=xyzuser;PWD=xyzpassword;DATABASE=xyzdatabase\" + (txtTaxYear + \";\")");

        objConn.Open();

        ADODB.Recordset objRs= new ADODB.RecordsetClass();

        //var objRs = Server.CreateObject("ADODB.Recordset");

        // -------------------------------------------------
        // Execute SQL Function to determine Property Type
        // -------------------------------------------------

        ADODB.Recordset rsRci= new ADODB.RecordsetClass();

        //var rsRci = Server.CreateObject("ADODB.Recordset");

        var mysql = ("select h_" + (txtTaxYear + (".dbo.hc_fn_rci(\'" + (webacct + "\') AS rci"))));


        //while (!(rsRci.State == 1))
        while (rsRci.State != (int) ConnectionState.Open)
            rsRci.Open(mysql, objConn);

        while ((!rsRci.BOF) & (!rsRci.EOF))
        {
            straptype == rsRci("rci");
            switch (straptype)
            {
                case null:
                    straptype = "N";
                    break;
                case "":
                    straptype = "B";
                    break;
            }
            rsRci.MoveNext();
        }


        object i;
        object j;
        strap = "";

        if ((straptype == "C"))
        {
            var strSql = ("EXEC hcad..sp_maps_HE_getSubjectAcctInfo \'" + (txtTaxYear + ("\', \'" + (webacct + "\', \'C\'"))));
            objRs.Open(strSql, objConn);
            if (!objRs.EOF)
            {               
                strap = objRs("strap");                
                ownerName = objRs("ownerName");
                siteAddr = objRs("site_addr");
                nhCd = objRs("nh_cd");
                yrImpr = objRs("yr_impr");
                dorCd = objRs("dor_cd");
                facetMap = objRs("facet_map").Substring(0, 4);                
            }
        }


        // -------------------------------------------------
        // Clean Up Objects
        // -------------------------------------------------
        if (rsRci.State == 1)
        {
            rsRci.Close();
        }

        if (objRs.State == 1)
        {
            objRs.Close();
        }


        rsRci = null;
        objRs = null;
        objConn = null;
    }
}

    </script>
</head>

更新7/31/17:

我接受了Alex和Chetan的建议并重新编写了代码,只使用ADO.Net和SQL Connections和Commands。我现在可以使用SqlDataReader将变量分配给前一个记录集objRs存储过程变量(现在是一个SqlCommand)。

1 个答案:

答案 0 :(得分:0)

我接受了Alex和Chetan的建议并重新编写了代码,只使用ADO.Net和SQL Connections和Commands。我现在可以使用SqlDataReader将变量分配给前一个记录集objRs存储过程变量(现在是一个SqlCommand)。