NHibernate执行存储过程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaouncle/article/details/82896167

既然要学习NHibernate,那知道如何执行存储过程自然是必不可少的,网上有很多资料是用新建映射文件的方式在NHibernate中执行存储过程,我个人觉得这真的不太好,反复琢磨后搞出了下面的这种方式,希望对大家有帮助。

一、NHibernate执行insert存储过程(无返回值)

CREATE PROCEDURE [dbo].[proc_AddSysDict]
	@Id uniqueidentifier,
	@DictCode nvarchar(50),
	@DictName nvarchar(50),
	@ParentCode uniqueidentifier
AS
BEGIN
	SET NOCOUNT ON;

        -- Insert statements for procedure here
	insert into Sys_Dict(Id, DictCode, DictName, ParentCode, Remarks,CreateTime) values(@Id,@DictCode,@DictName,@ParentCode,'proc备注',GETDATE())
END
public ActionResult Index()
{
    #region 执行存储过程
    using (IDbConnection connection = new NHibernateHelper().OpenSession().Connection)
    {
        IDbTransaction transaction = connection.BeginTransaction();
        using (IDbCommand command = connection.CreateCommand())
        {
            try
            {
                command.Transaction = transaction;
                #region 执行Insert操作,无返回值
                command.CommandText = "proc_AddSysDict";
                command.CommandType = CommandType.StoredProcedure;
                
                IDbDataParameter pp_Id = command.CreateParameter();
                pp_Id.DbType = DbType.Guid;
                pp_Id.ParameterName = "Id";
                pp_Id.Value = Guid.NewGuid();
                pp_Id.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_Id);

                IDbDataParameter pp_DictCode = command.CreateParameter();
                pp_DictCode.DbType = DbType.String;
                pp_DictCode.ParameterName = "DictCode";
                pp_DictCode.Value = "90002";
                pp_DictCode.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_DictCode);

                IDbDataParameter pp_DictName = command.CreateParameter();
                pp_DictName.DbType = DbType.String;
                pp_DictName.ParameterName = "DictName";
                pp_DictName.Value = "proc车辆型号";
                pp_DictName.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_DictName);

                IDbDataParameter pp_ParentCode = command.CreateParameter();
                pp_ParentCode.DbType = DbType.String;
                pp_ParentCode.ParameterName = "ParentCode";
                pp_ParentCode.Value = "cbb2cdb6-1ebe-44b2-970f-a95a04fc169b";
                pp_ParentCode.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_ParentCode);

                command.ExecuteNonQuery();
                #endregion

                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
    }
    #endregion

    return View();
}

二、NHibernate执行insert存储过程(返回主键)

CREATE PROCEDURE [dbo].[proc_AddSysDict02]
	@DictCode nvarchar(50),
	@DictName nvarchar(50),
	@ParentCode uniqueidentifier
AS
BEGIN
	SET NOCOUNT ON;
	declare @Id uniqueidentifier;
	set @Id = newid();
        -- Insert statements for procedure here
	insert into Sys_Dict(Id, DictCode, DictName, ParentCode, Remarks,CreateTime) values(@Id,@DictCode,@DictName,@ParentCode,'proc备注',GETDATE());
	select @Id
END
public ActionResult Index()
{
    #region 执行存储过程
    using (IDbConnection connection = new NHibernateHelper().OpenSession().Connection)
    {
        IDbTransaction transaction = connection.BeginTransaction();
        using (IDbCommand command = connection.CreateCommand())
        {
            try
            {
                command.Transaction = transaction;
                #region 执行Insert操作,返回主键
                command.CommandText = "proc_AddSysDict02";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Clear();

                pp_DictCode.DbType = DbType.String;
                pp_DictCode.ParameterName = "DictCode";
                pp_DictCode.Value = "90003";
                pp_DictCode.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_DictCode);

                pp_DictName.DbType = DbType.String;
                pp_DictName.ParameterName = "DictName";
                pp_DictName.Value = "proc车辆型号";
                pp_DictName.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_DictName);

                pp_ParentCode.DbType = DbType.String;
                pp_ParentCode.ParameterName = "ParentCode";
                pp_ParentCode.Value = "cbb2cdb6-1ebe-44b2-970f-a95a04fc169b";
                pp_ParentCode.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_ParentCode);

                string result = command.ExecuteScalar().ToString();
                #endregion

                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
    }
    #endregion

    return View();
}

三、NHibernate执行delete存储过程

CREATE PROCEDURE [dbo].[proc_RemoveSysDict]
	@DictName nvarchar(50),
	@ParentCode uniqueidentifier	
AS
BEGIN
	-- 若设置了set nocount on则无论删除了多少行都返回-1
	-- SET NOCOUNT ON;

        -- Insert statements for procedure here
	delete from Sys_Dict where ParentCode=@ParentCode and DictName like '%'+@DictName+'%';
END
public ActionResult Index()
{
    #region 执行存储过程
    using (IDbConnection connection = new NHibernateHelper().OpenSession().Connection)
    {
        IDbTransaction transaction = connection.BeginTransaction();
        using (IDbCommand command = connection.CreateCommand())
        {
            try
            {
                command.Transaction = transaction;
                #region 执行Delete操作
                command.CommandText = "proc_RemoveSysDict";
                command.CommandType = CommandType.StoredProcedure;

                IDbDataParameter pp_DictName = command.CreateParameter();
                pp_DictName.DbType = DbType.String;
                pp_DictName.ParameterName = "DictName";
                pp_DictName.Value = "部门";
                pp_DictName.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_DictName);

                IDbDataParameter pp_ParentCode = command.CreateParameter();
                pp_ParentCode.DbType = DbType.String;
                pp_ParentCode.ParameterName = "ParentCode";
                pp_ParentCode.Value = "cbb2cdb6-1ebe-44b2-970f-a95a04fc169b";
                pp_ParentCode.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_ParentCode);

                //存储过程中不要设置SET NOCOUNT ON;,否则无论删除了多少行返回值都是-1
                int count = command.ExecuteNonQuery();
                #endregion

                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
    }
    #endregion

    return View();
}

四、NHibernate执行update存储过程

CREATE PROCEDURE [dbo].[proc_UpdateSysDict]
	-- Add the parameters for the stored procedure here
	@OldParentCode uniqueidentifier,
	@NewParentCode uniqueidentifier
AS
BEGIN
	-- 若设置了set nocount on则无论删除了多少行都返回-1
	-- SET NOCOUNT ON;

        -- Insert statements for procedure here
	update Sys_Dict set ParentCode=@NewParentCode where ParentCode=@OldParentCode
END
public ActionResult Index()
{
    #region 执行存储过程
    using (IDbConnection connection = new NHibernateHelper().OpenSession().Connection)
    {
        IDbTransaction transaction = connection.BeginTransaction();
        using (IDbCommand command = connection.CreateCommand())
        {
            try
            {
                command.Transaction = transaction;
                #region 执行Update操作
                command.CommandText = "proc_UpdateSysDict";
                command.CommandType = CommandType.StoredProcedure;

                IDbDataParameter pp_OldParentCode = command.CreateParameter();
                pp_OldParentCode.DbType = DbType.String;
                pp_OldParentCode.ParameterName = "OldParentCode";
                pp_OldParentCode.Value = "781782ad-0fe1-4cd3-961c-aaaaaaaaaaaa";
                pp_OldParentCode.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_OldParentCode);

                IDbDataParameter pp_NewParentCode = command.CreateParameter();
                pp_NewParentCode.DbType = DbType.String;
                pp_NewParentCode.ParameterName = "NewParentCode";
                pp_NewParentCode.Value = "781782ad-0fe1-4cd3-961c-bbbbbbbbbbbb";
                pp_NewParentCode.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_NewParentCode);

                //存储过程中不要设置SET NOCOUNT ON;,否则无论修改了多少行返回值都是-1
                int count = command.ExecuteNonQuery();
                #endregion

                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
    }
    #endregion

    return View();
}

五、NHibernate执行select存储过程

CREATE PROCEDURE [dbo].[proc_GetSysDict]
	@ParentCode UNIQUEIDENTIFIER,
	@DictName NVARCHAR(50)
AS
BEGIN
	SET NOCOUNT ON;

        -- Insert statements for procedure here
	select Id, DictCode, DictName, ParentCode, Remarks, CreateTime from Sys_Dict where ParentCode=@ParentCode and DictName like '%'+@DictName+'%';
	select @@ROWCOUNT as TotalRowCount
END
public ActionResult Index()
{
    #region 执行存储过程
    using (IDbConnection connection = new NHibernateHelper().OpenSession().Connection)
    {
        IDbTransaction transaction = connection.BeginTransaction();
        using (IDbCommand command = connection.CreateCommand())
        {
            try
            {
                command.Transaction = transaction;
                #region 执行Select操作
                command.CommandText = "proc_GetSysDict";
                command.CommandType = CommandType.StoredProcedure;

                IDbDataParameter pp_DictName = command.CreateParameter();
                pp_DictName.DbType = DbType.String;
                pp_DictName.ParameterName = "DictName";
                pp_DictName.Value = "部门";
                pp_DictName.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_DictName);

                IDbDataParameter pp_ParentCode = command.CreateParameter();
                pp_ParentCode.DbType = DbType.String;
                pp_ParentCode.ParameterName = "ParentCode";
                pp_ParentCode.Value = "781782ad-0fe1-4cd3-961c-bbbbbbbbbbbb";
                pp_ParentCode.Direction = ParameterDirection.Input;
                command.Parameters.Add(pp_ParentCode);

                SqlDataAdapter adapter = new SqlDataAdapter((SqlCommand)command);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                DataTable list = ds.Tables[0];
                DataTable count = ds.Tables[1];
                #endregion

                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
    }
    #endregion

    return View();
}

猜你喜欢

转载自blog.csdn.net/xiaouncle/article/details/82896167