代码如下:
using System
using System.Collections.Generic
using System.Text
using System.Windows.Forms
using System.IO
namespace LoaMen.Com.Sql
{
/// <summary>
/// create by loamen http://www.loamen.com
/// </summary>
public class SqlServerHelper
{
string connectionString = string.Empty
string dbServer, dbUser, dbPassword, dbName
private string _fileName
/// <summary>
/// 文件全路径
/// </summary>
public string FileName
{
get { return _fileName}
set { _fileName = value}
}
public SqlServerHelper()
{
string[] connectionString = PubConstant.ConnectionString.Split('')
foreach (string str in connectionString)
{
if (str.ToLower().Contains("data source"))
{
dbServer = str.Split('=')[1]
}
else if (str.ToLower().Contains("initial catalog"))
{
dbName = str.Split('=')[1]
}
else if (str.ToLower().Contains("user id"))
{
dbUser = str.Split('=')[1]
}
else if (str.ToLower().Contains("pwd"))
{
dbPassword = str.Split('=')[1]
}
}
}
private bool checkDB()
{
if (!string.IsNullOrEmpty(dbServer) &&
!string.IsNullOrEmpty(dbName) &&
!string.IsNullOrEmpty(dbUser) &&
!string.IsNullOrEmpty(dbPassword))
{
return true
}
else
{
return false
}
}
/// <summary>
/// 数据库备份
/// </summary>
public void DbBackup()
{
if (checkDB())
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass()
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass()
try
{
oSQLServer.LoginSecure = false
oSQLServer.Connect(dbServer, dbUser, dbPassword)
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database
oBackup.Database = dbName
FileInfo fileInfo = new FileInfo(this.FileName)
oBackup.Files = fileInfo.FullName
oBackup.BackupSetName = dbName
oBackup.BackupSetDescription = "数据库备份"
oBackup.Initialize = true
oBackup.SQLBackup(oSQLServer)
}
catch (Exception ex)
{
throw ex
}
finally
{
oSQLServer.DisConnect()
}
}
else
{
MessageBoxEx.ShowInformationMessage("数据库连接有问题,无法备份!")
}
}
/// <summary>
/// 数据库恢复
/// </summary>
public void DbRestore()
{
if (checkDB())
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass()
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass()
try
{
oSQLServer.LoginSecure = false
oSQLServer.Connect(dbServer, dbUser, dbPassword)
SQLDMO.QueryResults qr = oSQLServer.EnumProcesses(-1)
int iColPIDNum = -1
int iColDbName = -1
for (int i = 1i <= qr.Columnsi++)
{
string strName = qr.get_ColumnName(i)
if (strName.ToUpper().Trim() == "SPID")
{
iColPIDNum = i
}
else if (strName.ToUpper().Trim() == "DBNAME")
{
iColDbName = i
}
if (iColPIDNum != -1 &&iColDbName != -1)
break
}
//杀死使用strDbName数据库的进程
for (int i = 1i <= qr.Rowsi++)
{
int lPID = qr.GetColumnLong(i, iColPIDNum)
string strDBName = qr.GetColumnString(i, iColDbName)
if (strDBName.ToUpper() == this.dbName.ToUpper())
{
oSQLServer.KillProcess(lPID)
}
}
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database
oRestore.Database = dbName
oRestore.Files = this.FileName
oRestore.FileNumber = 1
oRestore.ReplaceDatabase = true
oRestore.SQLRestore(oSQLServer)
}
catch (Exception ex)
{
throw ex
}
finally
{
oSQLServer.DisConnect()
}
}
else
{
MessageBoxEx.ShowInformationMessage("数据库连接有问题,无法还原!")
}
}
}
}
asp在线备份sql server数据库:1、备份
<%
SQL="backup database 数据库名 to disk='"&Server.MapPath("backup")&"\"&"backuptext.dat"&"'"
set cnn=Server.createobject("adodb.connection")
cnn.open "driver={SQL Server}Server=服务器名uid=sapwd="
cnn.execute SQL
on error resume next
if err<>0 then
response.write "错误:"&err.Descripting
else
response.write "数据备份成功!"
end if
%>
2、恢复
<%
SQL="Restore database 数据库名 from disk='"&Server.MapPath("backup")&"\"&"backuptext.dat"&"'"
set cnn=Server.createobject("adodb.connection")
cnn.open "driver={SQL Server}Server=服务器名uid=sapwd="
cnn.execute SQL
on error resume next
if err<>0 then
response.write "错误:"&err.Descripting
else
response.write "数据恢复成功!"
end if
%>
注:以上语句是把数据备份到磁盘的backup目录下,文件名为backuptext.dat。
2、ASP中能修改SQL数据库结构吗?
答:ALTER TABLE
ALTER TABLE — 更改表属性
语法
ALTER TABLE table [ * ]
ADD [ COLUMN ] column type
ALTER TABLE table [ * ]
ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }
ALTER TABLE table [ * ]
RENAME [ COLUMN ] column TO newcolumn
ALTER TABLE table
RENAME TO newtable
ALTER TABLE table
ADD table constraint definition
Inputs
table
试图更改的现存表的名称.
column
现存或新的列名称.
type
新列的类型.
newcolumn
现存列的新名称.
newtable
表的新名称.
table constraint definition
表的新的约束定义.
New table constraint for the table
输出
ALTER
从被更名的列或表返回的信息.
ERROR
如果一个列或表不存在返回的信息.
描述
ALTER TABLE 变更一个现存表的定义.ADD COLUMN 形式用与 CREATE TABLE一样的语法向表中增加一个新列/字段。ALTER COLUMN 形式允许你从列/字段中设置或者删除缺省(值)。注意缺省(值)只对新插入的行有效。RENAME 子句可以在不影响相关表中任何数据的情况下更改一个表或者列/字段的名称。因此,表或列/字段在此命令执行后仍将是相同尺寸和类型。ADD table constraint definition 子句用与 CREATE TABLE一样的语法向表中增加一个新的约束。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)