C# 在VisualStudio2010中使用ADOX会标记错误

C# 在VisualStudio2010中使用ADOX会标记错误,c#,.net,database,oledb,ado,C#,.net,Database,Oledb,Ado,根本原因是什么?您之所以会出现此错误,是因为您没有指定OLE DB提供程序,至少在您给出的代码示例中没有指定。添加对ADODB类型库的引用,然后在前面添加以下代码: Table table = new Table(); table.Name = "TestResults"; Column column = new Column(); column.Name = "SerialNumber"; column.Type = DataTypeEnum.adVarChar; column.Defined

根本原因是什么?

您之所以会出现此错误,是因为您没有指定OLE DB提供程序,至少在您给出的代码示例中没有指定。添加对ADODB类型库的引用,然后在前面添加以下代码:

Table table = new Table();
table.Name = "TestResults";
Column column = new Column();
column.Name = "SerialNumber";
column.Type = DataTypeEnum.adVarChar;
column.DefinedSize = 14;
table.Columns.Append(column);
catalog.Tables.Append(table); --- flags "Object is no longer valid" exception 
对于我的代码,我必须将列类型更改为adVarWChar,因为。但您可能正在使用另一个数据库

我用一个.mdb文件对此进行了测试,并成功地添加了新表(我可以肯定,因为当我再次运行它时,我发现错误“table'TestResults'已经存在”

Connection conn = new Connection();
string connectionString = "Provider='Microsoft.Jet.OLEDB.4.0'; Data Source='path-to-some-database-file.mdb'";
conn.Open(connectionString, "userid", "password", -1);
catalog.ActiveConnection = conn;