關於ADO.NET參數化查詢的提問

最近咱們的正式環境一直在報錯一個異常,首先我貼出來異常信息sql

BLL層 捕獲到了請求的url數據庫

  Net.BLL.MobileFun MobileFun.GetBusinessBidPolicy 異常數組

傳入參數爲:AucId=643129;CarId=680362;businessid:41773;async

異常信息爲:必須聲明標量變量 "@UID"。 必須聲明標量變量 "@UID"。 必須聲明標量變量 "@UID"。ide

URL:http://*********.com/httpHandler/ForMobileServiceHandler.ashx?action=getBidPolicy&aucId=643129&carId=680362&businessid=BC440DE0D6DCA7FD&version=v3.2.8&onlineid=021d162e-ba3e-45ac-8c59-8d265522d6ef&clienttype=0this

 

Dao層捕獲到的異常信息lua

  Net.DAL.AuctionDao 位置:Net.DAL.AuctionDao,方法:GetBidPolicy,錯誤信息:System.Data.SqlClient.SqlException (0x80131904): 必須聲明標量變量 "@UID"。 必須聲明標量變量 "@UID"。 必須聲明標量變量 "@UID"。 在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 在 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() 在 System.Data.SqlClient.SqlDataReader.get_MetaData() 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 在 System.Data.SqlClient.SqlCommand.ExecuteScalar() 在 Net.DBUtility.MySqlHelper.ExecuteScalar(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters) 在 Net.DBUtility.MySqlHelper.ExecuteScalar(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) 在 Net.DBUtility.MySqlDataProvider.GetSingle(String SQLString, IDataParameter[] cmdParms) 在 Net.DAL.AuctionDao.GetBidPolicy(String aucId) ClientConnectionId:48c2084f-c6b0-46b5-93b9-8a59ebcd29edurl

 

如今我貼出來 我這幾個方法spa

BLL層.net

        public string GetBusinessBidPolicy(string AucId, string CarId, string businessid)
        {
            ConvertToJson ctj = new ConvertToJson();
            try
            {
                int tag = -1;
                int Offer = 0;
                int price = 0;
                int priceNonlocal = 0;
                objAuctionDao.GetBusinessBidInfo(AucId, CarId, ref tag, ref Offer, ref  price, ref  priceNonlocal, businessid);
                ctj.Append("ResId", "0");
                ctj.Append("ResMsg", "獲取商戶出價方案和報價成功!");
                ctj.Append("BidWay", tag);//0:本市;1 外遷;-1 還沒有選擇出價方式
                ctj.Append("price", price);
                ctj.Append("priceNonlocal", priceNonlocal);
                ctj.Append("MyOptimizationOffer", Offer);
            }
            catch (Exception e)
            {
                ctj.Append("ResId", "1");
                ctj.Append("ResMsg", "出現異常,獲取商戶出價方案和報價失敗!");
                m_objLog.Error("MobileFun.GetBusinessBidPolicy 異常    傳入參數爲:AucId=" + AucId + ";CarId=" + CarId + ";businessid:" + businessid + ";異常信息爲:" + e.Message+"URL:"+HttpContext.Current.Request.Url);
            }
            return ctj.ToObject();
        }

DAO層

   public void GetBusinessBidInfo(string aucId, string CarId, ref int tag, ref int Offer, ref int price, ref int priceNonlocal, string businessID = "")
        {
          
            try
            {
                string busID = "";
                if (string.IsNullOrEmpty(businessID))
                    busID = new MemberDto().BusinessId;
                else
                    busID = businessID;
                //-1 須要出價承諾,其餘不須要車價承諾
                int tag1 = GetBidPolicy(aucId);
                if (tag1 != 1 && tag1 != 2 && tag1 != 4 && tag1 != 3)
                {
                    tag = 0; return;
                }

                //            _strSql = string.Format(@"SELECT TOP 1 Tag FROM tbAuction_BidPolicyNow with(nolock) WHERE AucId = {0} AND BusinessId = {1} ORDER BY Created DESC
                //                                    SELECT Offer FROM tbSHD_OptimizationQuote with(nolock)  WHERE BusinessID={1} AND AucID={0}
                //                                    SELECT TOP 1 ISNULL(price,0) price,ISNULL(priceNonlocal,0) priceNonlocal FROM tbAuction_Evaluate with(nolock)  WHERE businessId={1} AND carid={2}  ORDER BY created desc
                //                                    ", aucId, busID, CarId);
                _strSql =
                        @"SELECT TOP 1 Tag FROM tbAuction_BidPolicyNow with(nolock) WHERE AucId = @AucID AND BusinessId = @UID ORDER BY Created DESC
                                    SELECT Offer FROM tbSHD_OptimizationQuote with(nolock)  WHERE BusinessID=@UID AND AucID=@AucID 
                                    SELECT TOP 1 ISNULL(price,0) price,ISNULL(priceNonlocal,0) priceNonlocal FROM tbAuction_Evaluate with(nolock)  WHERE businessId=@UID  AND carid=@CarID  ORDER BY created desc
                                    ";
                SqlParameter[] par = { new SqlParameter("@AucID", aucId), new SqlParameter("@UID", businessID), new SqlParameter("@CarID", CarId) };
                DataSet obj = _loacaldbRead.Query(_strSql, par);
                if (obj != null && obj.Tables.Count > 0)
                {
                    if (obj.Tables[0].Rows.Count > 0)
                    {
                        tag = int.Parse(obj.Tables[0].Rows[0][0].ToString());
                    }
                    else
                    {
                        tag = -1;
                    }
                    if (obj.Tables[1].Rows.Count > 0)
                    {
                        Offer = int.Parse(obj.Tables[1].Rows[0][0].ToString());
                    }
                    if (obj.Tables[2].Rows.Count > 0)
                    {
                        price = int.Parse(obj.Tables[2].Rows[0][0].ToString());
                        priceNonlocal = int.Parse(obj.Tables[2].Rows[0][1].ToString());
                    }
                }
            }
            catch (Exception e)
            {
                string error = string.Format("位置:{0},方法:{1},錯誤信息:{2}", this.GetType().ToString(),
                    System.Reflection.MethodBase.GetCurrentMethod().Name, e);
                _log.Error(error);
                throw;

            }

 

       public int GetBidPolicy(string aucId)
        {
        //    _strSql = string.Format("SELECT nonlocaltag FROM tbauction_baseinfo with(nolock) WHERE id = {0}", aucId);

        
            try
            {
                _strSql = "SELECT nonlocaltag FROM tbauction_baseinfo with(nolock) WHERE id = @AucID";
                SqlParameter[] par = { new SqlParameter("@AucID", aucId) };
                object obj = _loacaldbRead.GetSingle(_strSql, par);
                if (obj == null) return -1;
                return Convert.ToInt32(obj);
            }
            catch (Exception e)
            {
                string error = string.Format("位置:{0},方法:{1},錯誤信息:{2}", this.GetType().ToString(),
                    System.Reflection.MethodBase.GetCurrentMethod().Name, e);
                _log.Error(error);
                throw;

            }

        }

 

我一直很奇怪 我用寫的參數化查詢語句  而後值初始化在參數數組中丟給ADO.NET執行,爲何會說我沒有聲明@UID這個變量呢?

並且還有一個異常 沒法找到表1 

 

GetBidPolicy  這個方法中根本沒有用到@UID的參數可是報錯卻說
Net.DAL.AuctionDao 位置:Net.DAL.AuctionDao,方法:GetBidPolicy,錯誤信息:System.Data.SqlClient.SqlException (0x80131904): 必須聲明標量變量 "@UID"。 必須聲明標量變量 "@UID"。 必須聲明標量變量 "@UID"。

如今能夠肯定 找不到表1 的這個異常是

                    if (obj.Tables[1].Rows.Count > 0)
                    {
                        Offer = int.Parse(obj.Tables[1].Rows[0][0].ToString());
                    }

這裏取 table[1].rows.count  的時候 發現dataset中 表1 null  因此拋出這個異常

那麼問題來了 個人三個查詢一塊兒執行 就算沒有查詢到數據  dataset 中也會有三張表 即使是空表 也不能沒有表

如今請教  各位 這個異常究竟是什麼緣由產生的呢?

URL 我本地,生產環境都試過了 直接敲出來沒有報錯異常..

另請教 參數化的sql語句  ado.net 去數據庫執行的時候 是如何把值賦給參數的 ,求詳解!

相關文章
相關標籤/搜索