时间SQL数据类型生成无效的变体异常

时间:2012-04-03 13:16:07

标签: c# sql sql-server-2008 ado.net sqldatatypes

我的环境如下:

SQL Server 2008 Visual C#Express在Windows 7上运行

我有一个存储过程,它返回许多字段,包括一个数据类型为'Time'的字段。当我尝试将此字段分配给变量时(在代​​码片段中使用了timepan,显然是使用的那个,但也尝试了datetime和具有相同结果的对象),编译器会给出以下MDA错误:

  

检测到InvalidVariant
  在从非托管VARIANT到托管对象的转换期间检测到无效变体。将无效的VARIANT传递给CLR可能会导致意外的异常,损坏或数据丢失。

非常感谢您提供的任何帮助。

代码如下:

private void CreateCScrapeObjects(string ItemsToScrapeStoredProc, int MaxItemsPerRequest, int SendRequestTolerance)
    {
        object tempobjecttotestnull;
        string scrapename;
        string scrapeurl;
        string scrapetemplate;
        string latestflag;
        string parameter1;
        string parameter2;
        string parameter3;
        string parameter1replacestring;
        string parameter2replacestring;
        string parameter3replacestring;
        int dataitemsperday;
        DateTime lastsuccessfulrequesttime;
        DateTime lastscrapeddatadate;
        TimeSpan scrapetime;

        DateTime startdate;
        DateTime startdateforrequestitem;
        DateTime enddate;
        DateTime enddateforrequestitem;

        int latestdateoffset;
        string scrapefrequencytype;
        int scrapefrequencynumber;
        string rescrapefrequencytype;
        int rescrapefrequencynumber;


        Scrapeobject MyScrape = new Scrapeobject ();
        List<ADODB.Parameter> oParams = new List<ADODB.Parameter>();
        ADODB.Recordset oRst = new ADODB.Recordset();
        if (_databasequeue.RunStoredProcedure(ItemsToScrapeStoredProc, , oParams, _connectstring, ref oRst, ADODB.ExecuteOptionEnum.adOptionUnspecified))
        {
            while (!oRst.EOF)
            {
                scrapename = (string)oRst.Fields[0].Value;
                scrapeurl = (string)oRst.Fields[1].Value;
                scrapetemplate = (string)oRst.Fields[2].Value;
                latestflag = (string)oRst.Fields[3].Value;

                tempobjecttotestnull = oRst.Fields[5].Value;
                if (tempobjecttotestnull.Equals(System.DBNull.Value))
                { parameter1 = ""; }
                else
                { parameter1 = (string)tempobjecttotestnull; }

                tempobjecttotestnull = oRst.Fields[7].Value;
                if (tempobjecttotestnull.Equals(System.DBNull.Value))
                { parameter2 = ""; }
                else
                { parameter2 = (string)tempobjecttotestnull; }

                tempobjecttotestnull = oRst.Fields[9].Value;
                if (tempobjecttotestnull.Equals(System.DBNull.Value))
                { parameter3 = ""; }
                else
                { parameter3 = (string)tempobjecttotestnull; }

                tempobjecttotestnull = oRst.Fields[4].Value;
                if (tempobjecttotestnull.Equals(System.DBNull.Value))
                { parameter1replacestring = ""; }
                else
                { parameter1replacestring = (string)tempobjecttotestnull; }

                tempobjecttotestnull = oRst.Fields[6].Value;
                if (tempobjecttotestnull.Equals(System.DBNull.Value))
                { parameter2replacestring = ""; }
                else
                { parameter2replacestring = (string)tempobjecttotestnull; }

                tempobjecttotestnull = oRst.Fields[8].Value;
                if (tempobjecttotestnull.Equals(System.DBNull.Value))
                { parameter3replacestring = ""; }
                else
                { parameter3replacestring = (string)tempobjecttotestnull; }

                dataitemsperday = (int)oRst.Fields[14].Value;
                latestdateoffset = (int)oRst.Fields[15].Value;
                scrapefrequencytype = (string)oRst.Fields[10].Value;
                scrapefrequencynumber = (int)oRst.Fields[11].Value;
                rescrapefrequencytype = (string)oRst.Fields[12].Value;
                rescrapefrequencynumber = (int)oRst.Fields[13].Value;



                scrapetime = TimeSpan.Parse( Convert.ToString ( oRst.Fields[16].Value));
                lastsuccessfulrequesttime = Convert.ToDateTime(oRst.Fields[17].Value);
                lastscrapeddatadate = Convert.ToDateTime(oRst.Fields[18].Value);

                startdate = GetNextScrapeDate(scrapefrequencytype, scrapefrequencynumber, lastscrapeddatadate,scrapetime );
                enddate = DateTime.Now.AddDays(latestdateoffset);

                startdateforrequestitem = startdate;
                enddateforrequestitem = GetScrapeRequestEndDate(scrapefrequencytype,scrapefrequencynumber,lastscrapeddatadate,enddate,MaxNGCDataItemsPerRequest,scrapetime);

                while(startdateforrequestitem<enddate)
                {

                    if (MyScrape == null)
                    {
                        MyScrape = new ScrapeObject();
                        MyScrape .DefineScrape(startdateforrequestitem, enddateforrequestitem, scrapeurl, scrapetemplate);
                        MyScrape .AddItemToRequest(latestflag, parameter1, parameter2, parameter3, parameter1replacestring, parameter2replacestring, parameter3replacestring);
                    }
                    else
                    {
                        int expecteddataitems = ScrapeFrequency.ExpectedDataItems(scrapefrequencytype,scrapefrequencynumber,startdateforrequestitem,enddateforrequestitem,scrapetime);
                        if (expecteddataitems > MyScrape .MinRemainingScrapeItems(MaxItemsPerRequest ))
                        {
                            _scrapequeue.AddQueueItem(MyScrape );
                            MyScrape = null;
                            MyScrape = new ScrapeObject();
                            MyScrape .DefineScrape(startdateforrequestitem, enddateforrequestitem, scrapeurl, scrapetemplate);
                            MyScrape .AddItemToRequest(latestflag, parameter1, parameter2, parameter3, parameter1replacestring, parameter2replacestring, parameter3replacestring);
                        }
                        else
                        {
                            MyScrape .AddItemToRequest(latestflag, parameter1, parameter2, parameter3, parameter1replacestring, parameter2replacestring, parameter3replacestring);
                        }

                    }


                }
            }
            if (MyScrape != null)
            {
                _scrapequeue.AddQueueItem(MyScrape );
            }
        }
        else
        {
            //raise error
        }

    }

1 个答案:

答案 0 :(得分:0)

我使用的是错误版本的adodb(我一直在使用旧的com版本,因为它工作正常,我觉得不需要重新发明轮子)。尽管如此,在这种情况下我应该更新,因为Time SQL数据类型是新的并且(显然)旧的adodb版本不支持。我重新编写了所有数据库交互以使用adodb.net,并且它与Timespan类型一起正常工作。