我一直在尝试创建Access 2010数据库几天,每次尝试都会遇到各种错误。我已经安装了驱动程序,并且我已经引用了ADOX和ADODB。我遇到的最新错误是0x80040E21,它是"发生了OLE DB错误。错误代码:0x80040E21。 OLE DB记录可用。来源:" Microsoft Native Client" Hresult:0x80040E21描述:"多步OLE DB操作产生错误。检查每个OLE DB状态值(如果可用)。没有工作。"。 无法创建OLE DB访问器。验证列元数据是否有效。"
我不完全确定这个错误的原因。我的整个程序如下。我只想简单地创建一个包含1个表的数据库,并用几列填充它,并设置列的数据类型和条目的大小。有没有人对我失踪的东西有任何想法?
我也想帮忙弄清楚如何设置ID列的自动增量。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADOX;
using ADODB;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CreateNewAccessDatabase(@"C:\Users\owner\Desktop\Reports.accdb");
}
public bool CreateNewAccessDatabase(string fileName)
{
bool result = false;
ADOX.Catalog cat = new ADOX.Catalog();
ADOX.Table table = new ADOX.Table();
table.Name = "ServerReports";
ADOX.Column ID = new ADOX.Column();
ID.Type = ADOX.DataTypeEnum.adBigInt;
ID.DefinedSize = 19;
ID.Name = "ID";
ADOX.Column INDEX = new ADOX.Column();
INDEX.Type = ADOX.DataTypeEnum.adInteger;
INDEX.DefinedSize = 10;
INDEX.Name = "Index";
ADOX.Column NAME = new ADOX.Column();
NAME.Type = ADOX.DataTypeEnum.adVarChar;
NAME.DefinedSize = 256;
NAME.Name = "Name";
ADOX.Column ADDRESS = new ADOX.Column();
ADDRESS.Type = ADOX.DataTypeEnum.adVarChar;
ADDRESS.DefinedSize = 256;
ADDRESS.Name = "Address";
ADOX.Column STATUS = new ADOX.Column();
STATUS.Type = ADOX.DataTypeEnum.adBoolean;
STATUS.DefinedSize = 1;
STATUS.Name = "Status";
ADOX.Column PING = new ADOX.Column();
PING.Type = ADOX.DataTypeEnum.adDouble;
PING.DefinedSize = 9;
PING.Name = "Ping";
ADOX.Column DATE = new ADOX.Column();
DATE.Type = ADOX.DataTypeEnum.adDate;
DATE.Name = "Date";
table.Columns.Append(ID);
table.Columns.Append(INDEX);
table.Columns.Append(NAME);
table.Columns.Append(ADDRESS);
table.Columns.Append(STATUS);
table.Columns.Append(PING);
table.Columns.Append(DATE);
table.Keys.Append("ID", ADOX.KeyTypeEnum.adKeyPrimary, "ID", "ServerReports", "ID");
try
{
cat.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Persist Security Info=False;");
cat.Tables.Append(table);
ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
if (con != null)
con.Close();
textBox1.Text = "Database Created Successfully";
result = true;
}
catch (Exception ex)
{
textBox1.Text = Convert.ToString(ex);
result = false;
}
cat = null;
return result;
}
}
}