1

我想从 oracle 数据库中获取多个数据并将其存储到 C# .net 中的另一个变量中。

我正在使用此代码。

public static void FetchData(string MeterSerialNumber, string MeterManufacturer, string MeterType, XmlDocument xmlReXML, string StartTime, string StopTime)

    {
        string Acctid = "";
        string Ser = "";
        string name1 = "";
        string towns = "";
        string Qry = "SELECT DISTINCT AC.ACCOUNTID as accid ,MT.SERIALNUMBER as srno,AC.NAME as names,OC.OPCONAME as town1,OC.CITY discm FROM ACCOUNT AC, XXMETERHIST M, LSMDPHYSICALMETER MT,LSSERVICEPOINT P,LSMDMTRMANUFACTURER MANUFAC,OPERATINGCOMPANY OC,LSMARKET LS WHERE AC.UIDACCOUNT = M.UIDACCOUNT AND M.UIDPHYSICALMETER = MT.UIDPHYSICALMETER and LS.MARKETID = OC.OPCOCODE and LS.UIDMARKET = P.UIDMARKET and MANUFAC.UIDMTRMANUFACTURER = MT.UIDMETERMANUFACTURER AND M.UIDSERVICEPOINT = P.UIDSERVICEPOINT and MT.SERIALNUMBER ='?' AND MT.UIDPHYSMTRTYPE = '43'";
        OleDbConnection con = new OleDbConnection("provider=MSDAORA.oracle;user id=testapp;password=testapp;data source=mdmdb_tb");
        DataTable dtbl = new DataTable();
        OleDbDataAdapter dad = new OleDbDataAdapter(Qry, con);
        dad.SelectCommand.Parameters.Add("?", OleDbType.VarChar).Value = MeterSerialNumber;
        con.Open();

        dtbl.Columns.Add("accid");
        dtbl.Columns.Add("srno");
        dtbl.Columns.Add("names");
        dtbl.Columns.Add("town1");
        dtbl.Columns.Add("discm");
        dad.Fill(dtbl);
       if (dtbl.Rows.Count > 0)
      {
            Acctid = (dtbl.Rows[0].ItemArray[0].ToString());
            Ser = (dtbl.Rows[0].ItemArray[1].ToString());
            name1 = (dtbl.Rows[0].ItemArray[2].ToString());
            towns = (dtbl.Rows[0].ItemArray[3].ToString());

      }
    }

但是数据并没有保存在所有变量中。它抛出了boundrange异常的Indexout。

4

1 回答 1

1

不确定这是否是问题的根源,但这可能会从 SQL 查询中为您提供一些奇怪的数据:

你的意思是OC.CITY AS discm代替OC.CITY discm吗?

此外,只是建议您可能希望为数据表的列指定类型:

dtbl.Columns.Add("accid", typeof(int));
dtbl.Columns.Add("srno", typeof(int));
dtbl.Columns.Add("names", typeof(string));

等等

于 2013-08-14T16:05:17.037 回答