[c#]代码库
using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SEMApp
{
class Program
{
/// <summary>
/// Main函数
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//connection string
string __ConnectString = "SERVER=.;DATABASE=TestA;UID=sa;PWD=xxx";
SMO smo = new SMO(__ConnectString);
smo.DatabaseName = "TestA";
smo.Connect();
//1>备份数据库
//BackupDatabase(smo);
//2>获取数据库所有表
//ForeachTable(smo);
//3>循环数据库
ForeachDatabase(smo);
Console.ReadLine();
}
/// <summary>
/// 备份数据库>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>测试通过
/// </summary>
/// <param name="smo"></param>
static void BackupDatabase(SMO smo)
{
smo.backupPath = @"D:\backup\";
smo.BackupDatabase();
Console.WriteLine("备份成功!");
}
/// <summary>
/// 循环数据所有表
/// </summary>
/// <param name="smo"></param>
static void ForeachTable(SMO smo)
{
Console.WriteLine(string.Format("循环数据库下所有用户表:"));
List<Table> list = smo.Tables.Cast<Table>().Where((e) => e.IsSystemObject == false).ToList();
foreach (Table table in list)
{
//循环列
for (int i = 0; i < table.Columns.Count; i++)
{
Column column = table.Columns[i];
//标示列:column.Identity;
//column.IdentityIncrement;//自增量
//column.IdentitySeed;//种子
//column.InPrimaryKey;//主键
//column.Nullable;//可空
}
Console.WriteLine(table.Name);
}
}
/// <summary>
/// SMO
/// </summary>
/// <param name="smo"></param>
static void ForeachDatabase(SMO smo)
{
foreach (Database database in smo.Databases)
{
Console.WriteLine(database.Name);
}
}
}
}
by: 发表于:2017-12-20 17:29:46 顶(0) | 踩(0) 回复
??
回复评论