插入不正常ADO.NET [英] Insert Not working correctly ADO.NET

查看:73
本文介绍了插入不正常ADO.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我非常沮丧,因为这个SP在SSMS中可以正常执行,但是使用我的项目将执行5次中的1次。我只是要发布我的代码,看看有没有人看到我不知道的东西。我正在使用Sql Azure DB:



This has me so frustrated, because this SP works fine executing in SSMS, but will execute 1 out of 5 times using my project. I am just going to post my code and see if anyone sees something I don't. I am using Sql Azure DB:

//get variables needed
MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
Guid newUserGuid = (Guid)newUser.ProviderUserKey;

            //Insert Initial Registration data
SqlConnection cn = new    SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
SqlCommand cmd = new SqlCommand("dbo.spInsertInitialUserConfirmationInfo", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@fkUserId", newUserGuid.ToString());
            cn.Open();
            cmd.BeginExecuteNonQuery();
            cn.Close();

推荐答案

我需要添加cmd.EndExecuteQuery();声明如下:





I needed to add the cmd.EndExecuteQuery(); statement see below:


cn.Open();
            IAsyncResult result = cmd.BeginExecuteNonQuery();
            try
            {
                cmd.EndExecuteNonQuery(result);
                Response.Redirect("~/MyAccount");
            }
            catch (Exception ex)
            {
                errorsOccured.Text = "An error occurred: " + ex.Message;
            }
            finally
            {
                cn.Close();
            }


每次调用proc时都必须清除cmd参数然后再次添加参数,

检查链接:

在ADO.NET中调用存储过程 [ ^ ]

http://www.csharp-station.com/Tutorials/ AdoDotNet / Lesson07.aspx [ ^ ]

调用存储使用ADO.NET连接的程序 [ ^ ]
You must clear your cmd parameters each time you call proc then add parameters again,
check the links:
Calling Stored procedures in ADO.NET[^]
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx[^]
Calling a stored procedure using ADO.NET connection[^]


这篇关于插入不正常ADO.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆