C#动态创建数据库
2013-04-10 15:15
316 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADOX;
namespace WfpApp
{
/// <summary>
/// 创建库需要添加COM引用:Microsoft ADO Ext. 2.8 for DDL and Security
/// 创建数据表需要添加COM引用:Microsoft ActiveX Data Objects 2.8 Library
/// </summary>
public class DynamicAccess
{
public DynamicAccess(string local)
{
_DbPath = local;
}
private string _DbPath;
/// <summary>
/// ACCESS数据库路径,包含文件名称
/// </summary>
public string DbPath
{
get { return _DbPath; }
set { _DbPath = value; }
}
private string _strErrorInfo;
/// <summary>
/// 获取异常信息
/// </summary>
/// <returns></returns>
public string GetStrErrorInfo()
{
return _strErrorInfo;
}
/// <summary>
/// 动态创建ACCESS
/// </summary>
/// <returns></returns>
public bool CreateAccess()
{
try
{
ADOX.Catalog catalog = new ADOX.Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath + ";Jet OLEDB:Engine Type=5");
return true;
}
catch (Exception ex)
{
_strErrorInfo = ex.Message;
return false;
}
}
public bool CreateTable()
{
try
{
ADOX.Catalog catalog = new ADOX.Catalog();
//创建链接
ADODB.Connection cn = new ADODB.Connection();
//打开
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath, null, null, -1);
//激活链接
catalog.ActiveConnection = cn;
//创建表
ADOX.Table table = new ADOX.Table();
table.Name = "FirstTable";
//创建列
ADOX.Column column = new ADOX.Column();
column.ParentCatalog = catalog;
//列名称
column.Name = "RecordId";
//列类型
column.Type = DataTypeEnum.adInteger;
//默认长度
column.DefinedSize = 9;
//自动增长列
column.Properties["AutoIncrement"].Value = true;
//将列添加到表中
table.Columns.Append(column, DataTypeEnum.adInteger, 9);
//第一列为主键
table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
table.Columns.Append("CustomerName", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Age", DataTypeEnum.adInteger, 9);
table.Columns.Append("Birthday", DataTypeEnum.adDate, 0);
catalog.Tables.Append(table);
cn.Close();
return true;
}
catch (Exception ex)
{
_strErrorInfo = ex.Message;
return false;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADOX;
namespace WfpApp
{
/// <summary>
/// 创建库需要添加COM引用:Microsoft ADO Ext. 2.8 for DDL and Security
/// 创建数据表需要添加COM引用:Microsoft ActiveX Data Objects 2.8 Library
/// </summary>
public class DynamicAccess
{
public DynamicAccess(string local)
{
_DbPath = local;
}
private string _DbPath;
/// <summary>
/// ACCESS数据库路径,包含文件名称
/// </summary>
public string DbPath
{
get { return _DbPath; }
set { _DbPath = value; }
}
private string _strErrorInfo;
/// <summary>
/// 获取异常信息
/// </summary>
/// <returns></returns>
public string GetStrErrorInfo()
{
return _strErrorInfo;
}
/// <summary>
/// 动态创建ACCESS
/// </summary>
/// <returns></returns>
public bool CreateAccess()
{
try
{
ADOX.Catalog catalog = new ADOX.Catalog();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath + ";Jet OLEDB:Engine Type=5");
return true;
}
catch (Exception ex)
{
_strErrorInfo = ex.Message;
return false;
}
}
public bool CreateTable()
{
try
{
ADOX.Catalog catalog = new ADOX.Catalog();
//创建链接
ADODB.Connection cn = new ADODB.Connection();
//打开
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath, null, null, -1);
//激活链接
catalog.ActiveConnection = cn;
//创建表
ADOX.Table table = new ADOX.Table();
table.Name = "FirstTable";
//创建列
ADOX.Column column = new ADOX.Column();
column.ParentCatalog = catalog;
//列名称
column.Name = "RecordId";
//列类型
column.Type = DataTypeEnum.adInteger;
//默认长度
column.DefinedSize = 9;
//自动增长列
column.Properties["AutoIncrement"].Value = true;
//将列添加到表中
table.Columns.Append(column, DataTypeEnum.adInteger, 9);
//第一列为主键
table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
table.Columns.Append("CustomerName", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("Age", DataTypeEnum.adInteger, 9);
table.Columns.Append("Birthday", DataTypeEnum.adDate, 0);
catalog.Tables.Append(table);
cn.Close();
return true;
}
catch (Exception ex)
{
_strErrorInfo = ex.Message;
return false;
}
}
}
}
相关文章推荐
- C#动态创建数据库并只允许特定用户登录该数据库
- C#动态创建Gridview及批量插入到数据库
- c#中动态创建textbox并且从数据库中获取表中数据添加到textbox中
- 第七章 业务数据库的管理(五)-- C#动态创建控件
- C#中动态创建数据库和数据表,很经典【转】
- 动态创建数据库和表(C#)
- C#动态创建数据库相关知识
- C#中创建和使用资源动态链接库收藏
- C# 利用反射动态创建对象
- C#中动态创建控件及事件处理程序
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- C# 反射动态创建窗体
- 动态手势识别--创建数据库(一)
- C#中创建和使用资源动态链接库
- C#动态创建及连接ACCESS数据库的详细步骤
- c#从数据库读取数据动态生成树形菜单
- 用C#从数据库动态生成AdminLTE菜单的一种方法
- C# CodeFirst(EF框架)代码优先创建数据库
- c#程序动态配置访问那种数据库
- C#动态执行字符串(动态创建代码)的实例代码