C#备份、还原MSSQL数据库问题!
开发环境:x64位系统,VS2008,MSSQL2008,网上说sqldml.dll组件可以完成,我测试后,一直提示错误。注册组件提示成功的!提示内容如下:“检索COM类...
开发环境:x64位系统,VS2008,MSSQL2008,网上说sqldml.dll组件可以完成,我测试后,一直提示错误。注册组件提示成功的!提示内容如下:
“检索 COM 类工厂中 CLSID 为 {10021F00-E260-11CF-AE68-00AA004A34D5} 的组件时失败,原因是出现以下错误: 80040154。”
各位高手帮忙看看是什么原因,或者有什么好的方法推荐下,感谢! 展开
“检索 COM 类工厂中 CLSID 为 {10021F00-E260-11CF-AE68-00AA004A34D5} 的组件时失败,原因是出现以下错误: 80040154。”
各位高手帮忙看看是什么原因,或者有什么好的方法推荐下,感谢! 展开
2013-08-06
展开全部
使用c#实现备份和还原数据库的方法如下: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ } /// <summary>
/// 备份数据库
/// </summary>
/// <param name="backupFolder">备份文件路径</param>
/// <returns></returns>
public static bool DataBackupConfigDB(string backupFolder)
{
//获取配置文件中sql数据库名
string dbName = "SqlDB";
string name = dbName + DateTime.Now.ToString("yyyyMMddHHmmss");
string procname;
string sql;
//创建连接对象
SqlConnection conn = new SqlConnection(GetConnStr());
conn.Open();//打开数据库连接
//删除逻辑备份设备,但不会删掉备份的数据库文件
procname = "sp_dropdevice";
SqlCommand sqlcmd1 = new SqlCommand(procname, conn);
sqlcmd1.CommandType = CommandType.StoredProcedure;
SqlParameter sqlpar = new SqlParameter();
sqlpar = sqlcmd1.Parameters.Add("@logicalname", SqlDbType.VarChar, 20);
sqlpar.Direction = ParameterDirection.Input;
sqlpar.Value = dbName;
try//如果逻辑设备不存在,略去错误
{
sqlcmd1.ExecuteNonQuery();
}
catch
{
//MessageBox.Show("错误的备份文件目录");
}
//创建逻辑备份设备
procname = "sp_addumpdevice";
SqlCommand sqlcmd2 = new SqlCommand(procname, conn);
sqlcmd2.CommandType = CommandType.StoredProcedure;
sqlpar = sqlcmd2.Parameters.Add("@devtype", SqlDbType.VarChar, 20);
sqlpar.Direction = ParameterDirection.Input;
sqlpar.Value = "disk";
sqlpar = sqlcmd2.Parameters.Add("@logicalname", SqlDbType.VarChar, 20);//逻辑设备名
sqlpar.Direction = ParameterDirection.Input;
sqlpar.Value = dbName;
sqlpar = sqlcmd2.Parameters.Add("@physicalname", SqlDbType.NVarChar, 260);//物理设备名
sqlpar.Direction = ParameterDirection.Input;
sqlpar.Value = backupFolder + name + ".bak";
try
{
int i = sqlcmd2.ExecuteNonQuery();
}
catch (Exception err)
{
string str = err.Message;
}
//备份数据库到指定的数据库文件(完全备份)
sql = "BACKUP DATABASE " + dbName + " TO " + dbName + " WITH INIT";
SqlCommand sqlcmd3 = new SqlCommand(sql, conn);
sqlcmd3.CommandType = CommandType.Text;
try
{
sqlcmd3.ExecuteNonQuery();
}
catch (Exception err)
{
string str = err.Message; conn.Close();
return false;
}
conn.Close();//关闭数据库连接
return true;
} /// <summary>
/// 还原数据库文件
/// </summary>
/// <param name="dbFile">数据库备份文件(含路径)</param>
/// <returns></returns>
public static bool DataRestoreConfigDB(string dbFile)
{
//sql数据库名
string dbName = "SqlDB";
//创建连接对象
SqlConnection conn = new SqlConnection(GetConnStr());
//还原指定的数据库文件
string sql = string.Format(@"
use master ;
declare @s varchar(8000);
select @s=isnull(@s,'')+' kill '+rtrim(spID) from master..sysprocesses where dbid=db_id('{0}');
select @s;exec(@s) ;RESTORE DATABASE {1} FROM DISK = N'{2}' with replace",
dbName, dbName, dbFile);
SqlCommand sqlcmd = new SqlCommand(sql, conn);
sqlcmd.CommandType = CommandType.Text;
conn.Open();
try { sqlcmd.ExecuteNonQuery(); }
catch (Exception err)
{ string str = err.Message;
conn.Close();
return false;
}
conn.Close();//关闭数据库连接
return true;
} private static string GetConnStr()
{
return "server=.;uid=sa;pwd=sa;database=db_test";
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ } /// <summary>
/// 备份数据库
/// </summary>
/// <param name="backupFolder">备份文件路径</param>
/// <returns></returns>
public static bool DataBackupConfigDB(string backupFolder)
{
//获取配置文件中sql数据库名
string dbName = "SqlDB";
string name = dbName + DateTime.Now.ToString("yyyyMMddHHmmss");
string procname;
string sql;
//创建连接对象
SqlConnection conn = new SqlConnection(GetConnStr());
conn.Open();//打开数据库连接
//删除逻辑备份设备,但不会删掉备份的数据库文件
procname = "sp_dropdevice";
SqlCommand sqlcmd1 = new SqlCommand(procname, conn);
sqlcmd1.CommandType = CommandType.StoredProcedure;
SqlParameter sqlpar = new SqlParameter();
sqlpar = sqlcmd1.Parameters.Add("@logicalname", SqlDbType.VarChar, 20);
sqlpar.Direction = ParameterDirection.Input;
sqlpar.Value = dbName;
try//如果逻辑设备不存在,略去错误
{
sqlcmd1.ExecuteNonQuery();
}
catch
{
//MessageBox.Show("错误的备份文件目录");
}
//创建逻辑备份设备
procname = "sp_addumpdevice";
SqlCommand sqlcmd2 = new SqlCommand(procname, conn);
sqlcmd2.CommandType = CommandType.StoredProcedure;
sqlpar = sqlcmd2.Parameters.Add("@devtype", SqlDbType.VarChar, 20);
sqlpar.Direction = ParameterDirection.Input;
sqlpar.Value = "disk";
sqlpar = sqlcmd2.Parameters.Add("@logicalname", SqlDbType.VarChar, 20);//逻辑设备名
sqlpar.Direction = ParameterDirection.Input;
sqlpar.Value = dbName;
sqlpar = sqlcmd2.Parameters.Add("@physicalname", SqlDbType.NVarChar, 260);//物理设备名
sqlpar.Direction = ParameterDirection.Input;
sqlpar.Value = backupFolder + name + ".bak";
try
{
int i = sqlcmd2.ExecuteNonQuery();
}
catch (Exception err)
{
string str = err.Message;
}
//备份数据库到指定的数据库文件(完全备份)
sql = "BACKUP DATABASE " + dbName + " TO " + dbName + " WITH INIT";
SqlCommand sqlcmd3 = new SqlCommand(sql, conn);
sqlcmd3.CommandType = CommandType.Text;
try
{
sqlcmd3.ExecuteNonQuery();
}
catch (Exception err)
{
string str = err.Message; conn.Close();
return false;
}
conn.Close();//关闭数据库连接
return true;
} /// <summary>
/// 还原数据库文件
/// </summary>
/// <param name="dbFile">数据库备份文件(含路径)</param>
/// <returns></returns>
public static bool DataRestoreConfigDB(string dbFile)
{
//sql数据库名
string dbName = "SqlDB";
//创建连接对象
SqlConnection conn = new SqlConnection(GetConnStr());
//还原指定的数据库文件
string sql = string.Format(@"
use master ;
declare @s varchar(8000);
select @s=isnull(@s,'')+' kill '+rtrim(spID) from master..sysprocesses where dbid=db_id('{0}');
select @s;exec(@s) ;RESTORE DATABASE {1} FROM DISK = N'{2}' with replace",
dbName, dbName, dbFile);
SqlCommand sqlcmd = new SqlCommand(sql, conn);
sqlcmd.CommandType = CommandType.Text;
conn.Open();
try { sqlcmd.ExecuteNonQuery(); }
catch (Exception err)
{ string str = err.Message;
conn.Close();
return false;
}
conn.Close();//关闭数据库连接
return true;
} private static string GetConnStr()
{
return "server=.;uid=sa;pwd=sa;database=db_test";
}
}
}
2013-08-06
展开全部
打下sp1试下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询