通过 SMO 运行 sql server 备份后创建事务时出现异常

发布于 2024-12-26 13:34:33 字数 4036 浏览 0 评论 0原文

每次在使用 SMO(Sql Server 管理对象)备份数据库后尝试创建新事务时,我都会收到以下错误:

不允许新事务,因为会话中还有其他线程正在运行。

我创建了一个小控制台应用程序来演示该问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using System.Data.SqlClient;
using System.Data;

namespace TestSMO
{
    class Program
    {
        string username = "radandba";
        string password = "belmont";
        string servername = "e2idev\\e2isqlexpress";
        string databaseName = "e2i";
        string backupfilename = "c:\\e2i.bak";
        Server server;

        static void Main(string[] args)
        {
            Program prog = new Program();
            prog.server = new Server(prog.servername);
            prog.server.ConnectionContext.LoginSecure = false;
            prog.server.ConnectionContext.Password = prog.password;
            prog.server.ConnectionContext.Login = prog.username;

            try
            {
                prog.server.ConnectionContext.Connect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                return;
            }

            Console.WriteLine("Connected");
            //prog.SelectData();

            Backup backup = new Backup();
            backup.Action = BackupActionType.Database;
            backup.Database = prog.databaseName;
            backup.Devices.Clear();
            backup.Incremental = false;
            backup.Devices.AddDevice(prog.backupfilename, DeviceType.File);
            backup.BackupSetName = prog.databaseName + " database backup";
            backup.BackupSetDescription = prog.databaseName + " database - Full backup";
            backup.Initialize = true;
            backup.CopyOnly = true;
            backup.LogTruncation = BackupTruncateLogType.NoTruncate;
            backup.CompressionOption = BackupCompressionOptions.Default;

            backup.Complete += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(prog.backup_Complete);
            backup.Information += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(prog.backup_Information);
            backup.PercentComplete += new PercentCompleteEventHandler(prog.backup_PercentComplete);
            backup.SqlBackupAsync(prog.server);
        }

        void backup_PercentComplete(object sender, PercentCompleteEventArgs e)
        {
            Console.WriteLine("Backup progress: " + e.Percent);
        }

        void backup_Information(object sender, Microsoft.SqlServer.Management.Common.ServerMessageEventArgs e)
        {
            Console.WriteLine("Backup status: No:" + e.Error.Number + " Detail: " + e.Error.Message);
        }

        void backup_Complete(object sender, Microsoft.SqlServer.Management.Common.ServerMessageEventArgs e)
        {
            Console.WriteLine("Backup status: No:" + e.Error.Number + " Detail: " + e.Error.Message);

            SelectData();
        }

        public void SelectData()
        {
            SqlConnection sqlCon = server.ConnectionContext.SqlConnectionObject;

            try
            {
                if (sqlCon.State != System.Data.ConnectionState.Open)
                    sqlCon.Open();

                SqlTransaction trans = sqlCon.BeginTransaction();
                SqlDataAdapter adapter = new SqlDataAdapter("select * from version", sqlCon);
                adapter.SelectCommand.Transaction = trans;
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                foreach (DataRow row in dt.Rows)
                {
                    Console.WriteLine(row[0]);
                }
                trans.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                sqlCon.Close();
            }
            Console.ReadLine();
        }
    }
}

I keep on getting the following error every time i try to create a new transaction after i have backed up my database using SMO (Sql Server Management Objects):

New transaction is not allowed because there are other threads running in the session.

I have created a little console app that demonstrates the problem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using System.Data.SqlClient;
using System.Data;

namespace TestSMO
{
    class Program
    {
        string username = "radandba";
        string password = "belmont";
        string servername = "e2idev\\e2isqlexpress";
        string databaseName = "e2i";
        string backupfilename = "c:\\e2i.bak";
        Server server;

        static void Main(string[] args)
        {
            Program prog = new Program();
            prog.server = new Server(prog.servername);
            prog.server.ConnectionContext.LoginSecure = false;
            prog.server.ConnectionContext.Password = prog.password;
            prog.server.ConnectionContext.Login = prog.username;

            try
            {
                prog.server.ConnectionContext.Connect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                return;
            }

            Console.WriteLine("Connected");
            //prog.SelectData();

            Backup backup = new Backup();
            backup.Action = BackupActionType.Database;
            backup.Database = prog.databaseName;
            backup.Devices.Clear();
            backup.Incremental = false;
            backup.Devices.AddDevice(prog.backupfilename, DeviceType.File);
            backup.BackupSetName = prog.databaseName + " database backup";
            backup.BackupSetDescription = prog.databaseName + " database - Full backup";
            backup.Initialize = true;
            backup.CopyOnly = true;
            backup.LogTruncation = BackupTruncateLogType.NoTruncate;
            backup.CompressionOption = BackupCompressionOptions.Default;

            backup.Complete += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(prog.backup_Complete);
            backup.Information += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(prog.backup_Information);
            backup.PercentComplete += new PercentCompleteEventHandler(prog.backup_PercentComplete);
            backup.SqlBackupAsync(prog.server);
        }

        void backup_PercentComplete(object sender, PercentCompleteEventArgs e)
        {
            Console.WriteLine("Backup progress: " + e.Percent);
        }

        void backup_Information(object sender, Microsoft.SqlServer.Management.Common.ServerMessageEventArgs e)
        {
            Console.WriteLine("Backup status: No:" + e.Error.Number + " Detail: " + e.Error.Message);
        }

        void backup_Complete(object sender, Microsoft.SqlServer.Management.Common.ServerMessageEventArgs e)
        {
            Console.WriteLine("Backup status: No:" + e.Error.Number + " Detail: " + e.Error.Message);

            SelectData();
        }

        public void SelectData()
        {
            SqlConnection sqlCon = server.ConnectionContext.SqlConnectionObject;

            try
            {
                if (sqlCon.State != System.Data.ConnectionState.Open)
                    sqlCon.Open();

                SqlTransaction trans = sqlCon.BeginTransaction();
                SqlDataAdapter adapter = new SqlDataAdapter("select * from version", sqlCon);
                adapter.SelectCommand.Transaction = trans;
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                foreach (DataRow row in dt.Rows)
                {
                    Console.WriteLine(row[0]);
                }
                trans.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                sqlCon.Close();
            }
            Console.ReadLine();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫竹語嫣☆ 2025-01-02 13:34:33

You could try to enable MARS on connection (msdn about MARS)
In connection string should be: "MultipleActiveResultSets=true;"

因为看清所以看轻 2025-01-02 13:34:33

正如 Anders UP 所说,结果是 NoLogTruncation 影响了它。

As stated by Anders UP turned out it was the NoLogTruncation was affecting it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文