首页 新闻 会员 周边 捐助

如何使用Sqldatasorce调用存储过程(sp_delete)并传递参数(@id int) ?

0
悬赏园豆:50 [待解决问题]
 如何使用Sqldatasorce调用存储过程(sp_delete)并传递参数(@id int) ?
问题补充:

如何使用事物(SqlTransaction)来处理下面的命令:SqlCommand sc = new SqlCommand("delete from t_user where userid=1",sqlConn);

chinese915的主页 chinese915 | 初学一级 | 园豆:149
提问于:2012-04-24 22:09
< >
分享
所有回答(4)
0
artwl | 园豆:16736 (专家六级) | 2012-04-24 22:10
0

存储过程

SqlCommand sc = new SqlCommand("exec sp_delete "+id,sqlConn);

SqlCommand sc = new SqlCommand("开始事务 delete from t_user where userid=1 提交事务",sqlConn);

az235 | 园豆:8483 (大侠五级) | 2012-04-25 08:21

帮顶。

支持(0) 反对(0) 小材小用 | 园豆:639 (小虾三级) | 2012-04-25 12:15
0

有两种方法,

一:采用.NET的类库实现事务处理:

private static void ExecuteSqlTransaction(string connectionString)
        
{
            
using (SqlConnection connection = new SqlConnection(connectionString))
            
{
                connection.Open();
                SqlCommand command 
= connection.CreateCommand();
                SqlTransaction transaction;  
                transaction 
= connection.BeginTransaction("SampleTransaction");
                command.Connection 
= connection;
                command.Transaction 
= transaction;
                
try
                
{
                    command.CommandText 
=
                        
"Insert into Department (ID, Name) VALUES (1, '工程部')";
                    command.ExecuteNonQuery();
                    command.CommandText 
=
                        
"Insert into Users(ID, Name,DepartmentID) VALUES (1, 'xyz',1)";
                    command.ExecuteNonQuery();
                    transaction.Commit();
                }

                
catch (Exception ex)
                
{
                   transaction.Rollback();
                }

            }

        }

二:写在SQL命令里, 用SQL实现

BEGIN TRANSACTION XXX
--sql Here

COMMIT TRANSACTION XXX
END

Chao Hong | 园豆:244 (菜鸟二级) | 2012-04-25 17:29
0

以上两位回答不错,帮顶。

小材小用 | 园豆:639 (小虾三级) | 2012-04-26 08:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册