连接字符串和sql备份

发布于 2024-12-08 20:36:06 字数 1308 浏览 0 评论 0原文

我在使用以下 C# 代码执行备份时遇到问题,特别是在连接字符串中。

代码如下:

private void BK()
{
    string strconn = @"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True"; 
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = strconn;

    try {
       //Query per backup
       string queryBK = "BACKUP DATABASE db TO DISK ='C:\\Program Files\\Microsoft SQLServer\\MSSQL10.SQLEXPRESS\\MSSQL\\Backup\\db.bak' WITH INIT, SKIP, CHECKSUM";

       SqlCommand cmdBK = new SqlCommand(queryBK, conn);
       conn.Open();            
       cmdBK.ExecuteNonQuery();
       MessageBox.Show("backup effettuato");
    }
    catch (Exception ex) {
       MessageBox.Show(ex.Message, "ERRORE", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally {
       conn.Close();
    }
 }

该代码在开发 PC 上运行,但如果我在另一台 PC 上安装应用程序,则会抛出此错误:

数据库不存在。验证名称是否已输入 正确。备份数据库异常中断。

我要强调的是,这个字符串与 INSERT、DELETE、UPDATE 操作配合得很好 在我的电脑和电脑测试上。

如果我将连接字符串替换为:

string strconn = @"Data Source=.\SQLEXPRESS; Database = db;Trusted_Connection =True";

该字符串可以在我的开发机器上运行,但不能在测试机器上运行。它抛出以下错误:

无法打开登录请求的数据库。登录失败。登录 用户 Pina-PC \ Pina 失败

I'm having problem with the following C# code to perform a backup, particularly in the connection string.

The code is as follows:

private void BK()
{
    string strconn = @"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True"; 
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = strconn;

    try {
       //Query per backup
       string queryBK = "BACKUP DATABASE db TO DISK ='C:\\Program Files\\Microsoft SQLServer\\MSSQL10.SQLEXPRESS\\MSSQL\\Backup\\db.bak' WITH INIT, SKIP, CHECKSUM";

       SqlCommand cmdBK = new SqlCommand(queryBK, conn);
       conn.Open();            
       cmdBK.ExecuteNonQuery();
       MessageBox.Show("backup effettuato");
    }
    catch (Exception ex) {
       MessageBox.Show(ex.Message, "ERRORE", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally {
       conn.Close();
    }
 }

This code works on the development PC, but if I install my application on another PC it throws this error:

The database does not exist. Verify that the name has been entered
correctly. INTERRUPTION ANOMALOUS BACKUP DATABASE.

I would stress that this string works well with the operations INSERT, DELETE, UPDATE
on both my PC and on the PC test.

If I replace the connection string with:

string strconn = @"Data Source=.\SQLEXPRESS; Database = db;Trusted_Connection =True";

The string works on my dev machine but not on the test machine. It throws the following error:

Can not open database requested by the login. Login failed. Login
failed for user Pina-PC \ Pina

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

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

发布评论

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

评论(1

温暖的光 2024-12-15 20:36:06

亲爱的朋友,您可以通过这种方式使用您的代码。

private void BK()
{
string strconn = @"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True"; 
SqlConnection conn = new SqlConnection();
conn.ConnectionString = strconn;

try {
// First get the db name

     conn.Open();

        string dbname;

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        dbname = cmd.Connection.Database.ToString();       


    //Query per backup
   string queryBK = "BACKUP DATABASE " + dbname + " TO DISK ='C:\\Program Files\\Microsoft SQLServer\\MSSQL10.SQLEXPRESS\\MSSQL\\Backup\\db.bak' WITH INIT, SKIP, CHECKSUM";

   SqlCommand cmdBK = new SqlCommand(queryBK, conn);
   conn.Open();            
   cmdBK.ExecuteNonQuery();
   MessageBox.Show("backup effettuato");
}
catch (Exception ex) {
   MessageBox.Show(ex.Message, "ERRORE", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally {
   conn.Close();
}
}

这个例程适用于您的情况。

Dear fellow you can use your code in this way.

private void BK()
{
string strconn = @"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True"; 
SqlConnection conn = new SqlConnection();
conn.ConnectionString = strconn;

try {
// First get the db name

     conn.Open();

        string dbname;

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        dbname = cmd.Connection.Database.ToString();       


    //Query per backup
   string queryBK = "BACKUP DATABASE " + dbname + " TO DISK ='C:\\Program Files\\Microsoft SQLServer\\MSSQL10.SQLEXPRESS\\MSSQL\\Backup\\db.bak' WITH INIT, SKIP, CHECKSUM";

   SqlCommand cmdBK = new SqlCommand(queryBK, conn);
   conn.Open();            
   cmdBK.ExecuteNonQuery();
   MessageBox.Show("backup effettuato");
}
catch (Exception ex) {
   MessageBox.Show(ex.Message, "ERRORE", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally {
   conn.Close();
}
}

this routine will work in your case.

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