仅执行一个查询 [英] Only One Query is Executing

查看:132
本文介绍了仅执行一个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Web项目.

在我的机器上(本地)正常运行.

但是当我在服务器上上传它时发生错误,


页面上有一个按钮和许多sql查询,但仅在上面(//---------------------注册代码---------- -------//)
该页面停止并且未在SQL DB中插入数据后,该部分仍在工作.

:(

I have created a web project.

which is working properly in my machine(Locally).

but When i Upload it on server error occur,


Page has a button and many sql queries on it but only above( //---------------------Sign up Code-----------------//)
section is working after that page stop and data is not being insert in SQL DB.

:(

推荐答案

一个方面是解决第二个查询"Insert into UserIncome"错误.您可以运行SQLProfiler来准确调试它在数据库层失败的原因.

第二方面是执行多个相关的SQL查询的最佳实践.如果未成功完成,.NET将提供一种很好的机制来回滚数据库.这是用于多个查询和回滚操作的代码.

One aspect is to resolve the second query ''Insert into UserIncome'' error. You can run SQLProfiler to debug exactly why it fails at DB layer.

Second aspect is the best practice on executing multiple related SQL queries. .NET provides nice mechanism to DB rollback, if not completed successfully. Here is the code for multiple queries and rollback operation.

SqlConnection conn = new SqlConnection(); //properly initiliased connection object with connection string
           SqlCommand command1 = new SqlCommand(); //properly initiliased command with sql text or procedure name
           SqlCommand command2 = new SqlCommand(); //properly initiliased command with sql text or procedure name

           SqlTransaction trx = conn.BeginTransaction();
           command1.Transaction = trx;
           command2.Transaction = trx;

           try
           {
               command1.ExecuteNonQuery();
               command2.ExecuteNonQuery();
               trx.Commit();
           }
           catch (SqlException ex)
           {
               trx.Rollback();
           }
           finally
           {
               //clean up resources
           }


您可以使用三个sqlcommand.

you can use three sqlcommand.

 string b= "INSERT INTO MyProfile (RefferedBy,UserName,MemberSince,Email)" + " VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', " + " getdate(), " + " '" + TextBox5.Text + "') ";

sqlcommand second=new sqlcommand(b,connection_object);
second.ExecutenonQuery();

string c="INSERT INTO UsersIncome (UserName,Level1Users,Level1PaidRef,Level2Users,Level2PaidRef,Level3Users,Level3PaidRef,Level4Users,Level4PaidRef,Level5Users,Level5PaidRef,TotalDownline,TotalIncome,TotalWithdraw,AvailBalance,TotalPurchasing,TotalPoints,UsedPoints,AvailablePoints,PendingWithdrawl,TotalPaidRef)" + " VALUES ( '" + TextBox2.Text + "',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) ";

sqlcommand Third=new Sqlcommand(c,connection_object);
third.ExecutenonQuery();

希望您的问题会得到解决....如果没有,请发布它..

Hope Your Problem Will Be solved.... If not Please Post it..


我认为,sqlCommand无法触发的原因2.和3. time是因为1.第一次使用带参数的命令,而第二次和第三次则没有.我看不到整个代码,但是我几乎可以肯定,您不会重置Sqlcommand.
更改2.和3.查询以将其与参数一起使用,并始终使用 Dispose() [<下次使用SqlCommand之前,请为SqlCommand使用href ="http://msdn.microsoft.com/zh-cn/library/z4956bkc" target ="_ blank" title ="New Window"> ^ ]方法.

I think, the reason that the sqlCommand not firing for the 2. and 3. time is because that for the 1. time you use command with parameters, but for the second and third time, you don''t. I don''t see the the whole code, but i''m almost sure, that you don''t reset your Sqlcommand.
Change the 2. and the 3. query to use it with parameters and always use Dispose()[^] method for SqlCommand, before next usage of SqlCommand.

//1. time usage
SqlCommand abc = new SqlCommand(...);
abc.ExecuteNonQuery();
abc.Dispose();

//2. time usage
SqlCommand abc = new SqlCommand(...);
abc.ExecuteNonQuery();
abc.Dispose();

//3. time usage
SqlCommand abc = new SqlCommand(...);
abc.ExecuteNonQuery();
abc.Dispose();


这篇关于仅执行一个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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