当前位置: 移动技术网 > IT编程>开发语言>.net > ADO.NET系列之事务和调用存储过程

ADO.NET系列之事务和调用存储过程

2019年01月15日  | 移动技术网IT编程  | 我要评论

库拉树,生活大爆炸第六季下载,柏安居

     前几篇我们介绍了connection、command和dataadapter等对象,本节我们将学习ado.net中不可缺少的事务,以及调用数据库的存储过程。

ado.net事务

     在许多大型、关键的应用程序中,计算机每秒钟都在执行大量的任务。更为经常的不是这些任务本身,而是将这些任务结合在一起完成一个业务要求,称为事务。如果能成功地执行一个任务,而在第二个或第三个相关的任务中出现错误,将会发生什么?这个错误很可能使系统处于不一致状态。这时事务变得非常重要,它能使系统摆脱这种不一致的状态。

     一个command对象的commandtext属性指定多条以;分割的语句。这种情况下若没有事务,所有的语句都会被执行,若其中有语句出错,就导致了数据的不一致性。当然我们也可以写存储过程,在sqlserver的数据库系统内建存储过程的语句若没有事务,多条语句中的部分语句失效,一样导致数据的不一致性:你可以在存储过程内部try/catch/begintransaction等。

     事务是一个最小的工作单元,不论成功与否都作为一个整体进行工作。详情可参考:

     下面我们做个示例展示事务在ado.net中的使用方法。

 string connectionstring = "data source=.;initial catalog=ax_log;user id=sa;password=sa123;";
            using (sqlconnection con = new sqlconnection(connectionstring))
            {
                con.open();
                using (sqltransaction tran = con.begintransaction())
                {
                    using (sqlcommand com = con.createcommand())
                    {
                        try
                        {
                            com.transaction = tran;
                            com.commandtext = "insert into table values('1','111')";
                            com.executenonquery();
                            com.commandtext = "insert into table values('2','222')";
                            com.executenonquery();
                            tran.commit();
                            console.writeline("事务提交成功");
                        }
                        catch (sqlexception ex)
                        {
                            tran.rollback();
                            con.close();
                            console.writeline("事务提交失败:" + ex.message);
                        }
                    }

                }

            }

    connection对象begintransaction启动事务,然后将事务赋值给command对象的transaction属性即挂接了事务。即使没有commit 和rollback,若执行中 错误,事务一样自动回滚,或者成功提交。

    begintransaction可以指定隔离级别。readxxx不会对数据库加锁,尽管在事务中,外部程序仍然可以读取数据;但若事务中有update语句,将导致数据库锁,外部程序不能继续读取数据。

     尽量考虑在存储过程中使用事务,避免使用ado的事务,因为ado的事务可能导致数据库长时间处于锁定状态;而数据库内的存储过程中的事务往往不会长时间挂起事务。

ado.net调用存储过程

     存储过程(stored procedure)是一组为了完成特定功能的t-sql语句集合,经编译后存储在sql server服务器中,利用存储过程可以加速sql语句的执行。

在应用程序中,使用存储过程读取数据,能够提高应用程序的工作效率,简化数据库的管理和显示信息

  创建一个带有输入参数的存储过程:

create procedure getuserpro
    @id int 
as
begin
    -- set nocount on added to prevent extra result sets from
    -- interfering with select statements.
    set nocount on;
    select * from dbo.eftest where id=@id
end
go

  ado.net中调用存储过程示例:

 string connectionstring = "data source=.;initial catalog=ax_log;user id=sa;password=sa123;";
            using (sqlconnection con = new sqlconnection(connectionstring))
            {
                string sql = "getuserpro";
                sqlparameter para = new sqlparameter("@id", 1);
                using (sqlcommand com = new sqlcommand(sql, con))
                {
                    dataset ds = new dataset();
                    try
                    {
                        com.parameters.add(para);
                        con.open();
                        com.commandtype = commandtype.storedprocedure;
                        sqldataadapter adapter = new sqldataadapter(com);
                        adapter.fill(ds);
                        foreach (datarow s in ds.tables[0].rows)
                        {
                            console.writeline("id:" + s["id"].tostring());
                            console.writeline("name:" + s["name"].tostring());
                        }
                    }
                    catch (exception ex)
                    { }
                }
            }

    看过command对象介绍的时候,代码跟那个差不多 ,是的  比command章节的代码就多了一行:com.commandtype = commandtype.storedprocedure; commandtype 默认情况是:com.commandtype = commandtype.text;commandtype.storedprocedure表示执行存储过程。

   

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网