使用 ADOX 创建 adDBTimeStamp 类型的字段时,会产生“类型无效”的错误提示,产生这种错误的原因是,数据库引擎 Provider 只能接受已知类型的数据类型,下面是可以接受的类型对照表:
ADOX.DataTypeEnum 类型枚举常量 | Value | Jet 3.51 | Jet 4.0 | SQL 7.0 |
---|---|---|---|---|
adBinary | 128 | Yes | Yes | Yes |
adBoolean | 11 | Yes | Yes | Yes |
adChar | 129 | Yes | No | Yes |
adCurrency | 6 | Yes | Yes | Yes |
adDate | 7 | Yes | Yes | No |
adDouble | 5 | Yes | Yes | Yes |
adGUID | 72 | Yes | Yes | Yes |
adInteger | 3 | Yes | Yes | Yes |
adLongVarBinary | 205 | Yes | Yes | Yes |
adLongVarChar | 201 | Yes | No | Yes |
adLongVarWChar | 203 | No | Yes | Yes |
adNumeric | 131 | No | Yes (with info)* | Yes (with info)* |
adSingle | 4 | Yes | Yes | Yes |
adSmallInt | 2 | Yes | Yes | Yes |
adUnsignedTinyInt | 17 | Yes | Yes | Yes |
adVarBinary | 204 | Yes | Yes | Yes |
adVarChar | 200 | Yes | No | Yes |
adVarWChar | 202 | No | Yes | Yes |
adWChar | 130 | No | Yes | Yes |
adDBTimeStamp | 135 | No | No | Yes |
下面就是一个例子:
C# 代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ADODB;
using ADOX;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CrateDBTable();
}
public void CrateDBTable()
{
String Table1 = "Category", Table2 = "User";
String ConnectStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://db1.mdb;";
ADODB.Connection cn = new ADODB.Connection();
cn.Open(ConnectStr, null, null, -1);
ADOX.Catalog cat = new ADOX.Catalog();
cat.ActiveConnection = cn;
ADOX.Table t; ;
//对于存在的表是否要进行删除
for (int i = cat.Tables.Count-1; i >-1; i--)
{
if (cat.Tables[i].Name.Equals(Table1, StringComparison.InvariantCultureIgnoreCase)
|| cat.Tables[i].Name.Equals(Table2, StringComparison.InvariantCultureIgnoreCase))
{
t = cat.Tables[i] as ADOX.Table;
cat.Tables.Delete(t.Name);
cat.Tables.Refresh();
}
}
ADOX.Table tbl = new ADOX.Table();
tbl.ParentCatalog = cat;
tbl.Name = Table1;
//增加一个自动增长的字段
ADOX.Column col = new ADOX.Column();
col.ParentCatalog = cat;
col.Type = ADOX.DataTypeEnum.adInteger; // 必须先设置字段类型
col.Name = "CategoryId";
col.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
col.Properties["AutoIncrement"].Value = true;
col.Properties["Nullable"].Value = true;
tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0);
tbl.Columns.Append("CategoryName", ADOX.DataTypeEnum.adVarWChar, 50);
//设置主键
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "CategoryId", "", "");
cat.Tables.Append(tbl);
//创建第二个表
ADOX.Table MyTable2 = new ADOX.Table();
MyTable2.ParentCatalog = cat;
MyTable2.Name = Table2;
//增加一个自动增长的字段
col = new ADOX.Column();
col.ParentCatalog = cat;
col.Type = ADOX.DataTypeEnum.adInteger; // 必须先设置字段类型
col.Name = "UserId";
col.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
col.Properties["AutoIncrement"].Value = true;
MyTable2.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0);
MyTable2.Columns.Append("UserName", ADOX.DataTypeEnum.adVarWChar, 50);
//下面这段将发生 “类型无效”的错误
/*
col = new ADOX.Column();
col.ParentCatalog = cat;
col.Type = ADOX.DataTypeEnum.adDBTimeStamp;
col.Properties["Nullable"].Value = true;
col.Name = "CreateDate";
MyTable2.Columns.Append("CreateDate", ADOX.DataTypeEnum.adDBTimeStamp,12);
*/
//正确的方法
MyTable2.Columns.Append("CreateDate", ADOX.DataTypeEnum.adDate,0);
//设置主键
MyTable2.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "UserId", "", "");
cat.Tables.Append(MyTable2);
cn.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl);
System.Runtime.InteropServices.Marshal.ReleaseComObject(MyTable2);
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat);
tbl = MyTable2 = null;
cat = null;
}
}
}