编程语言
首页 > 编程语言> > c# – Ajax webmethod无法在第一次尝试时触发存储过程

c# – Ajax webmethod无法在第一次尝试时触发存储过程

作者:互联网

我有一个webmethod在第一次尝试期间无法触发存储过程(我想在建立新连接时).我得到内部错误服务器作为错误Ajax消息.但是,如果我从URL地址返回返回,则执行存储过程并且页面可以正常工作,就像它们应该的那样.然后,如果我让页面保持一段时间不活动,然后尝试建立新连接,则会出现同样的问题.

我花了最近2天试图通过检查参数来识别问题,从web方法中删除大部分代码,最后我能够将问题跟踪到这个存储过程.

CREATE PROCEDURE [dbo].[spUnionServices]
@freetext NVARCHAR(50),
@offset int,
@fetch int

AS SET NOCOUNT ON;

BEGIN
SELECT IDphoto AS IDservice, photos.IDuser, photoCaption AS serviceTitle, photoDate as serviceDate, photoFileName AS servicePhoto, 'Photo' AS service,
    photoPublished as servicePublished, inap, hashtags, KEY_TBL.RANK, screenName AS IDname
    FROM photos INNER JOIN FREETEXTTABLE(photos, (photoCaption), @freetext) AS KEY_TBL ON photos.IDphoto = KEY_TBL.[KEY]
    INNER JOIN editorDetail ON editorDetail.IDuser = photos.IDuser
    ORDER BY RANK DESC OFFSET @offset ROWS FETCH NEXT @fetch ROWS ONLY  
END

这是我如何从web方法连接到存储过程

StringBuilder SBsmallSquares = new StringBuilder();
SqlConnection sqlConnection1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
           using (sqlConnection1)
           {
               SqlCommand cmd = new SqlCommand();
               SqlDataReader ReaderPopup;
               cmd.CommandText = "spUnionServices";
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Connection = sqlConnection1;
               cmd.Parameters.AddWithValue("@offset", offset);
               cmd.Parameters.AddWithValue("@fetch", fetch);
               cmd.Parameters.AddWithValue("@freetext", fts);

               sqlConnection1.Open();
               ReaderPopup = cmd.ExecuteReader();
               if (ReaderPopup.HasRows)
               {
                   while (ReaderPopup.Read())
                   {
                   //creating the string to return. Here there is no problem.
                   }
                   return SBsmallSquares.ToString();
               }
            else return string.Empty;

如果有人能够在第一次尝试运行存储过程时发现我遇到此问题,我将不胜感激.谢谢!

解决方法:

您应该重试此错误.在可用性事件(例如,升级)期间,DB可以移动到不同的机器,并且fdhost需要再次与SQL实例配对.通常,配对过程发生在第一个自由文本查询或需要时.您可能第一次看到该过程中的超时.它应该帮助你重试.如果这是非常一致的,那么服务中可能存在错误并让我们知道.我会帮你进一步调试.

标签:c,sql,asp-net,azure,azure-sql-database
来源: https://codeday.me/bug/20190711/1433489.html