namespace EC
{
/// <summary>
/// 数据库恢复和备份
/// </summary>
public class SqlBackObject
{
public SqlBackObject()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region SQL数据库备份
/// <summary>
/// SQL数据库备份
/// </summary>
/// <param name="ServerIP">SQL服务器IP或(Localhost)</param>
/// <param name="LoginName">数据库登录名</param>
/// <param name="LoginPass">数据库登录密码</param>
/// <param name="DBName">数据库名</param>
/// <param name="BackPath">备份到的路径</param>
public static void SQLBACK(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
#endregion
#region SQL恢复数据库
/// <summary>
/// SQL恢复数据库
/// </summary>
/// <param name="ServerIP">SQL服务器IP或(Localhost)</param>
/// <param name="LoginName">数据库登录名</param>
/// <param name="LoginPass">数据库登录密码</param>
/// <param name="DBName">要还原的数据库名</param>
public static void SQLDbRestore(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
{
SQLDMO.Restore orestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(ServerIP, LoginName, LoginPass);
orestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
orestore.Database = DBName;
orestore.Files = BackPath;
orestore.FileNumber = 1;
orestore.ReplaceDatabase = true;
orestore.SQLRestore(oSQLServer);
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
oSQLServer.DisConnect();
}
}
#endregion
#region 根据指定的文件名称创建Access数据库
/// <summary>
/// 根据指定的文件名称创建数据
/// </summary>
/// <param name="DBPath">绝对路径+文件名称</param>
}
#endregion
#region 压缩Access数据库
/// <summary>
/// 压缩Access数据库
/// </summary>
/// <param name="DBPath">数据库绝对路径</param>
public static void CompactAccess(string DBPath)
{
if (!File.Exists(DBPath))
{
throw new Exception("目标数据库不存在,无法压缩");
}
//声明临时数据库名称
string temp = DateTime.Now.Year.ToString();
temp += DateTime.Now.Month.ToString();
temp += DateTime.Now.Day.ToString();
temp += DateTime.Now.Hour.ToString();
temp += DateTime.Now.Minute.ToString();
temp += DateTime.Now.Second.ToString() + ".bak";
temp = DBPath.Substring(0, DBPath.LastIndexOf("\\") + 1) + temp;
#region 备份Access数据库
/// <summary>
/// 备份Access数据库
/// </summary>
/// <param name="srcPath">要备份的数据库绝对路径</param>
/// <param name="aimPath">备份到的数据库绝对路径</param>
/// <returns></returns>
public static void Backup(string srcPath,string aimPath)
{
if (!File.Exists(srcPath))
{
throw new Exception("源数据库不存在,无法备份");
}
try
{
File.Copy(srcPath,aimPath,true);
}
catch(IOException iXP)
#endregion
#region 还原Access数据库
/// <summary>
/// 还原Access数据库
/// </summary>
/// <param name="bakPath">备份的数据库绝对路径</param>
/// <param name="dbPath">要还原的数据库绝对路径</param>
public static void RecoverAccess(string bakPath,string dbPath)
{
if (!File.Exists(bakPath))
{
throw new Exception("备份数据库不存在,无法还原");
}
try
{
File.Copy(bakPath, dbPath, true);
}
catch (IOException ixp)
{
throw new Exception(ixp.ToString());
}
}
#endregion
}
}
===============================================================================================
请添加引用Microsoft ADO Ext. 2.7 for DDL and Securit...
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ADOX;
/// <summary>
/// file : create access db
/// autor: Wang Yahui
/// createtime : 2008-03-17
/// </summary>
public class CreateDB
{
/// <summary>
/// create access db
/// </summary>
/// <param name="Path">db file path</param>
public CreateDB(string Path)
{
//为了方便测试,数据库名字采用比较随机的名字,以防止添加不成功时还需要重新启动IIS来删除数据库。
string dbName = Path +DateTime.Now.Millisecond.ToString()+".mdb";
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";");
///创建StudentPaper表
ADOX.TableClass tbl = new ADOX.TableClass();
tbl.ParentCatalog = cat;
tbl.Name = Constant.StudentPaper;
///添加StudentID字段
ADOX.ColumnClass studentID = new ADOX.ColumnClass();
studentID.ParentCatalog = cat;
studentID.Type = ADOX.DataTypeEnum.adWChar; // 必须先设置字段类型
studentID.Name = "StudentID";
studentID.Properties["Jet OLEDB:Allow Zero Length"].Value = false;//是否可以为空
//col.Properties["AutoIncrement"].Value= true;//增加一个自动增长的字段
tbl.Columns.Append(studentID, ADOX.DataTypeEnum.adWChar, 20);
///增加PaperType字段
ADOX.ColumnClass paperType = new ADOX.ColumnClass();
paperType.ParentCatalog = cat;
paperType.Type = ADOX.DataTypeEnum.adWChar;
paperType.Name = "PaperType";
paperType.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(paperType, ADOX.DataTypeEnum.adWChar, 1);
//增加oMRAnswer字段
ADOX.ColumnClass oMRAnswer = new ADOX.ColumnClass();
oMRAnswer.ParentCatalog = cat;
oMRAnswer.Type = ADOX.DataTypeEnum.adWChar;
oMRAnswer.Name = "OMRAnswer";
oMRAnswer.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(oMRAnswer, ADOX.DataTypeEnum.adWChar, 255);
//增加oMRAnswer字段
ADOX.ColumnClass imageStudentID = new ADOX.ColumnClass();
imageStudentID.ParentCatalog = cat;
imageStudentID.Type = ADOX.DataTypeEnum.adWChar;
imageStudentID.Name = "ImageStudentID";
imageStudentID.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(imageStudentID, ADOX.DataTypeEnum.adWChar, 6);
///设置主键
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "StudentID", "", "");
cat.Tables.Append(tbl);
tbl = null;
cat = null;
} // end function CreateDB()
}
如果你创建数据库表时报“类型无效”的错误,请参考下表,对照一下字段的数据类型是否一致:
常数 | 值 | Jet 3.51 | Jet 4.0 | SQL 7.0 |
---|---|---|---|---|
adBinary | 128 | yes | yes | yes |
adBoolean | 11 | yes | yes | yes |
adChar | 129 | yes | 否 | yes |
adCurrency | 6 | yes | yes | yes |
adDate | 7 | yes | yes | 否 |
adDouble | 5 | yes | yes | yes |
adGUID | 72 | yes | yes | yes |
adInteger | 3 | yes | yes | yes |
adLongVarBinary | 205 | yes | yes | yes |
adLongVarChar | 201 | yes | 否 | yes |
adLongVarWChar | 203 | 否 | yes | yes |
adNumeric | 131 | 否 | 是的 (带有信息) * | 是的 (带有信息) * |
adSingle | 4 | yes | yes | yes |
adSmallInt | 2 | yes | yes | yes |
adUnsignedTinyInt | 17 | yes | yes | yes |
adVarBinary | 204 | yes | yes | yes |
adVarChar | 200 | yes | 否 | yes |
adVarWChar | 202 | 否 | yes | yes |
adWChar | 130 | 否 | yes | yes |
adDBTimeStamp | 135 | 否 | 否 | yes |