连接字符串和备份操作 [英] connection string and backup operation
问题描述
我是意大利语的程序员,我不太会英语.我希望能很好地解释我的问题.
我在装有Windows Seven Pro和vs 2010 pro,sql server 2008的PC上用C#开发.
备份操作和连接字符串有问题.
代码如下:
Hi,
I am a programmer Italian, I do not know English well. I hope to explain my problem well.
I develop in C # on a PC with Windows Seven Pro, vs 2010 pro, sql server 2008.
I have problems with the backup operation and 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";
//Creazione Command
SqlCommand cmdBK = new SqlCommand(queryBK, conn);
// Open connection.
conn.Open();
//Execute command
cmdBK.ExecuteNonQuery();
MessageBox.Show("backup effettuato");
}
catch (Exception ex)
{
// Process exception.
MessageBox.Show(ex.Message, "ERRORE", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
该代码可在开发PC上运行,但是如果我在另一台PC(使用Vista)上安装我的应用程序不起作用,则返回以下错误:
数据库不存在.请验证是否正确输入了名称.INTERRUPTION
异常备份数据库."
我要强调的是,此字符串与INSERT,DELETE,UPDATE
操作配合得很好.
在我的PC和PC测试上.
如果我将连接字符串替换为:
字符串strconn = @数据源=.\ SQLEXPRESS;数据库= db; Trusted_Connection = True";
该字符串在我的PC上有效,但在我的测试计算机上返回以下错误:
无法打开登录请求的数据库.登录失败.
用户Pina-PC \ Pina"
登录失败
有人可以帮助我解决这个问题吗?
谢谢... Iole
This code works on the development PC, but if I install my application on another PC (with Vista) does not work, returned the following 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 work on my PC but on my test computer returns the following error:
"Can not open database requested by the login. Login failed.
Login failed for user Pina-PC \ Pina "
Someone could help me to solve this problem?
Thanks...Iole
推荐答案
1)要备份数据库,则该数据库必须首先在另一台计算机上存在.
您的应用程序文件夹中必须有一个db.mdf
文件.
2)Integrated security
在数据库服务器以Windows模式而不是SQL Server模式工作时有效,因此您将获得"Pina-PC \ Pina"用户不存在或没有访问错误.
使用以下连接字符串:
1) For you to backup a database then that database must exist on the other machine first.
You must have adb.mdf
file in your application folder.
2)Integrated security
works when your database server is working in Windows mode not in SQL server mode, so you are getting the ''Pina-PC\Pina'' user does not exist or does not have access error.
use the following connection string :
string strconn = @"Data Source=.\SQLEXPRESS; Database = db;Trusted_Connection =True;integrated security = sspi;";
这篇关于连接字符串和备份操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!