在.NET 2.0框架下动态创建Access数据库和表时的注意事项
来源:互联网 发布:口袋妖怪xy存档数据坏 编辑:程序博客网 时间:2024/06/07 04:53
在以前的文章《如何在.NET框架下创建Access数据库和表?》中提供的方法,在.NET 2.0下无效,所有的字段类型都变成了文本类型,不知道微软改变了什么东西。提醒大家注意,需要加ADOX.ColumnClass.Type = DataTypeEnum.adLongVarBinary属性。下面将修正后的代码发布如下。
C#:
<%@ Page Language="C#" %>
<%@ Import Namespace="ADOX" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
/// <summary>
/// CreateAccessDB 的摘要说明。
/// 对于不同版本的ADO,需要添加不同的引用
/// 请添加引用Microsoft ADO Ext. 2.7 for DDL and Security
/// 请添加引用Microsoft ADO Ext. 2.8 for DDL and Security
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
//为了方便测试,数据库名字采用比较随机的名字,以防止添加不成功时还需要重新启动IIS来删除数据库。
string dbName = "D:/NewMDB" + DateTime.Now.Millisecond.ToString() + ".mdb";
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";");
Response.Write("数据库:" + dbName + "已经创建成功!");
ADOX.TableClass tbl = new ADOX.TableClass();
tbl.ParentCatalog = cat;
tbl.Name = "MyTable";
//增加一个自动增长的字段
ADOX.ColumnClass col = new ADOX.ColumnClass();
col.ParentCatalog = cat;
col.Type = ADOX.DataTypeEnum.adInteger; // 必须先设置字段类型
col.Name = "id";
col.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
col.Properties["AutoIncrement"].Value = true;
tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0);
//增加一个文本字段
ADOX.ColumnClass col2 = new ADOX.ColumnClass();
col2.ParentCatalog = cat;
col2.Name = "Description";
col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25);
//增加数字字段
ADOX.ColumnClass col3 = new ADOX.ColumnClass();
col3.ParentCatalog = cat;
col3.Name = "数字";
col3.Type = DataTypeEnum.adDouble;
col3.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(col3, ADOX.DataTypeEnum.adDouble, 666);
//增加Ole字段
ADOX.ColumnClass col4 = new ADOX.ColumnClass();
col4.ParentCatalog = cat;
col4.Name = "Ole类型";
col4.Type = DataTypeEnum.adLongVarBinary;
col4.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 0);
//设置主键
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", "");
cat.Tables.Append(tbl);
msg.Text = ("<br>数据库表:" + tbl.Name + "已经创建成功!");
System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl);
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat);
tbl = null;
cat = null;
GC.WaitForPendingFinalizers();
GC.Collect();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>在.NET框架下动态创建Access数据库和表</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="msg" runat="server" />
</form>
</body>
</html>
<%@ Import Namespace="ADOX" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
/// <summary>
/// CreateAccessDB 的摘要说明。
/// 对于不同版本的ADO,需要添加不同的引用
/// 请添加引用Microsoft ADO Ext. 2.7 for DDL and Security
/// 请添加引用Microsoft ADO Ext. 2.8 for DDL and Security
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
//为了方便测试,数据库名字采用比较随机的名字,以防止添加不成功时还需要重新启动IIS来删除数据库。
string dbName = "D:/NewMDB" + DateTime.Now.Millisecond.ToString() + ".mdb";
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";");
Response.Write("数据库:" + dbName + "已经创建成功!");
ADOX.TableClass tbl = new ADOX.TableClass();
tbl.ParentCatalog = cat;
tbl.Name = "MyTable";
//增加一个自动增长的字段
ADOX.ColumnClass col = new ADOX.ColumnClass();
col.ParentCatalog = cat;
col.Type = ADOX.DataTypeEnum.adInteger; // 必须先设置字段类型
col.Name = "id";
col.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
col.Properties["AutoIncrement"].Value = true;
tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0);
//增加一个文本字段
ADOX.ColumnClass col2 = new ADOX.ColumnClass();
col2.ParentCatalog = cat;
col2.Name = "Description";
col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25);
//增加数字字段
ADOX.ColumnClass col3 = new ADOX.ColumnClass();
col3.ParentCatalog = cat;
col3.Name = "数字";
col3.Type = DataTypeEnum.adDouble;
col3.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(col3, ADOX.DataTypeEnum.adDouble, 666);
//增加Ole字段
ADOX.ColumnClass col4 = new ADOX.ColumnClass();
col4.ParentCatalog = cat;
col4.Name = "Ole类型";
col4.Type = DataTypeEnum.adLongVarBinary;
col4.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 0);
//设置主键
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", "");
cat.Tables.Append(tbl);
msg.Text = ("<br>数据库表:" + tbl.Name + "已经创建成功!");
System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl);
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat);
tbl = null;
cat = null;
GC.WaitForPendingFinalizers();
GC.Collect();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>在.NET框架下动态创建Access数据库和表</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="msg" runat="server" />
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="ADOX" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dbName As String = "D:NewMDB" + DateTime.Now.Millisecond.ToString + ".mdb"
Dim cat As ADOX.CatalogClass = New ADOX.CatalogClass
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";")
Response.Write("数据库:" + dbName + "已经创建成功!")
Dim tbl As ADOX.TableClass = New ADOX.TableClass
tbl.ParentCatalog = cat
tbl.Name = "MyTable"
Dim col As ADOX.ColumnClass = New ADOX.ColumnClass
col.ParentCatalog = cat
col.Type = ADOX.DataTypeEnum.adInteger
col.Name = "id"
col.Properties("Jet OLEDB:Allow Zero Length").Value = False
col.Properties("AutoIncrement").Value = True
tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0)
Dim col2 As ADOX.ColumnClass = New ADOX.ColumnClass
col2.ParentCatalog = cat
col2.Name = "Description"
col2.Properties("Jet OLEDB:Allow Zero Length").Value = False
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25)
Dim col3 As ADOX.ColumnClass = New ADOX.ColumnClass
col3.ParentCatalog = cat
col3.Name = "数字"
col3.Type = DataTypeEnum.adDouble
col3.Properties("Jet OLEDB:Allow Zero Length").Value = False
tbl.Columns.Append(col3, ADOX.DataTypeEnum.adDouble, 666)
Dim col4 As ADOX.ColumnClass = New ADOX.ColumnClass
col4.ParentCatalog = cat
col4.Name = "Ole类型"
col4.Type = DataTypeEnum.adLongVarBinary
tbl.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 0)
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", "")
cat.Tables.Append(tbl)
msg.Text = ("<br>数据库表:" + tbl.Name + "已经创建成功!")
System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl)
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat)
tbl = Nothing
cat = Nothing
GC.WaitForPendingFinalizers()
GC.Collect()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>在.NET框架下动态创建Access数据库和表</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="msg" runat="server" />
</form>
</body>
</html>
<%@ Import Namespace="ADOX" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dbName As String = "D:NewMDB" + DateTime.Now.Millisecond.ToString + ".mdb"
Dim cat As ADOX.CatalogClass = New ADOX.CatalogClass
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";")
Response.Write("数据库:" + dbName + "已经创建成功!")
Dim tbl As ADOX.TableClass = New ADOX.TableClass
tbl.ParentCatalog = cat
tbl.Name = "MyTable"
Dim col As ADOX.ColumnClass = New ADOX.ColumnClass
col.ParentCatalog = cat
col.Type = ADOX.DataTypeEnum.adInteger
col.Name = "id"
col.Properties("Jet OLEDB:Allow Zero Length").Value = False
col.Properties("AutoIncrement").Value = True
tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0)
Dim col2 As ADOX.ColumnClass = New ADOX.ColumnClass
col2.ParentCatalog = cat
col2.Name = "Description"
col2.Properties("Jet OLEDB:Allow Zero Length").Value = False
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25)
Dim col3 As ADOX.ColumnClass = New ADOX.ColumnClass
col3.ParentCatalog = cat
col3.Name = "数字"
col3.Type = DataTypeEnum.adDouble
col3.Properties("Jet OLEDB:Allow Zero Length").Value = False
tbl.Columns.Append(col3, ADOX.DataTypeEnum.adDouble, 666)
Dim col4 As ADOX.ColumnClass = New ADOX.ColumnClass
col4.ParentCatalog = cat
col4.Name = "Ole类型"
col4.Type = DataTypeEnum.adLongVarBinary
tbl.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 0)
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", "")
cat.Tables.Append(tbl)
msg.Text = ("<br>数据库表:" + tbl.Name + "已经创建成功!")
System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl)
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat)
tbl = Nothing
cat = Nothing
GC.WaitForPendingFinalizers()
GC.Collect()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>在.NET框架下动态创建Access数据库和表</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="msg" runat="server" />
</form>
</body>
</html>
更多内容请到www.05yl.cn
- 在.NET 2.0框架下动态创建Access数据库和表时的注意事项
- 在.NET 2.0框架下动态创建Access数据库和表时的注意事项
- 在.NET 2.0框架下动态创建Access数据库和表时的注意事项
- 在.NET 2.0框架下动态创建Access数据库和表时的注意事项 (zz)
- 在.NET框架下动态创建Access数据库和表
- 在.NET 2.0框架下动态创建Access数据库和表
- 在.NET 2.0框架下动态创建Access数据库和表
- 在.NET 2.0框架下动态创建Access数据库和表
- 在.NET 2.0框架下动态创建Access数据库和表
- 如何在.NET框架下创建Access数据库和表
- 如何在.NET框架下创建Access数据库和表?
- .net中创建ACCESS数据库表的SQL和访问有密码ACCESS数据库的代码
- VB.Net中动态创建Access数据库
- VB.Net中动态创建Access数据库
- vc下动态创建access数据库的实现
- vc下动态创建access数据库的实现
- 在C# + Access + ADOX环境下创建数据库和数据表
- Access数据库在C#.net下的存储路径问题
- 涂涂评:读《不要祈求太多》有感
- DateChooser控件发布ASP.NET 2.0新版(我的ASP.NET 2.0控件开发书的第二个阶段项目)
- 处理sqlcommand里面的参数
- 创建、查询、修改带名称空间的 XML 文件的例子
- Linux程序设计——用getopt处理命令行参数
- 在.NET 2.0框架下动态创建Access数据库和表时的注意事项
- Autocad使用多线程,多核,多路CPU
- Visual Studio 2005 SDK 3.0 发布
- 急求如和将图片转成IMG格式???? 回QQ34140333
- 谈用Delphi设计Email程序
- IIS代码管理(2):创建应用程序池和属性
- WPF/E 简介
- 基于Web的RSS阅读器,新增加“加入收藏夹”功能,欢迎试用。
- 在.NET中实现彩色光标,动画光标和自定义光标