简体   繁体   English

如何检查MS Access数据库中是否存在指定的列?

[英]How do I check if specified column exists in MS Access database?

I need my program check if specified column exists in MS Access 2000 database, and if it doesn't - add it. 我需要我的程序检查MS Access 2000数据库中是否存在指定的列,如果不存在,请添加它。 I use .NET Framework 2.0 I tried to use oleDbConnection.GetSchema() method, but couldn't find column names in metadata (i'm really not a pro, huh) and any specification on msdn. 我使用.NET Framework 2.0,我尝试使用oleDbConnection.GetSchema()方法,但无法在元数据中找到列名(我不是专业人士,呵呵)以及msdn上的任何规范。 I would appreciate any help. 我将不胜感激任何帮助。

Thanks for answers. 感谢您的回答。 Here is solution i used in my code: 这是我在代码中使用的解决方案:

bool flag = false; string[] restrictions = new string[] { null, null, mytable };
DataTable dtColumns = oleDbConnection1.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, restrictions);
foreach (DataRow row in dtColumns.Rows) 
{ 
    if (mycolumnname==(string)row["COLUMN_NAME"]) flag = true;
}

This is code that is part of ao/r-mapper of mine. 这是我的ao / r-mapper的一部分的代码。 You cannot use it as is beacuse it depends on other classes, but I hope you get the picture. 您不能使用它,因为它依赖于其他类,但是我希望您有所了解。

Define restrictions like this 定义这样的限制

string[] restrictions = new string[] { null, null, tableName };

This retrieves the columns from a table 这将从表中检索列

private void RetrieveColumnInfo(OleDbConnection cnn, TableSchema tableSchema,
          string[] restrictions, Func<string, string> prepareColumnNameForMapping)
{
    using (DataTable dtColumns = 
                 cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, restrictions)) {
        string AutoNumberColumn = RetrieveAutoNumberColumn(cnn, tableSchema);
        foreach (DataRow row in dtColumns.Rows) {
            var col = new TableColumn();
            col.ColumnName = (string)row["COLUMN_NAME"];
            try {
                col.ColumnNameForMapping =
                    prepareColumnNameForMapping(col.ColumnName);
            } catch (Exception ex) {
                throw new UnimatrixExecutionException(
                    "Error in delegate 'prepareColumnNameForMapping'", ex);
            }
            col.ColumnAllowsDBNull = (bool)row["IS_NULLABLE"];
            col.ColumnIsIdentity = col.ColumnName == AutoNumberColumn;
            DbColumnFlags flags = (DbColumnFlags)(long)row["COLUMN_FLAGS"];
            col.ColumnIsReadOnly =
                col.ColumnIsIdentity ||
                (flags & (DbColumnFlags.Write | DbColumnFlags.WriteUnknown)) ==
                                                               DbColumnFlags.None;
            if (row["CHARACTER_MAXIMUM_LENGTH"] != DBNull.Value) {
                col.ColumnMaxLength = (int)(long)row["CHARACTER_MAXIMUM_LENGTH"];
            }
            col.ColumnDbType = GetColumnDbType((int)row["DATA_TYPE"]);
            col.ColumnOrdinalPosition = (int)(long)row["ORDINAL_POSITION"];
            GetColumnDefaultValue(row, col);

            tableSchema.ColumnSchema.Add(col);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查记录是否存在并在C#中的ms Access数据库中插入 - How to check if record exists or not and insert in ms access database in c# 如何检查 Access 数据库表中是否存在特定列 - How to check if specific column exists in an Access database table 如何检查MS Access数据库文件中是否已存在ID,以及如何在TextBox中使用TextChanged进行检查? - How to check if ID already exists in MS Access database file, and how to check it by using TextChanged in TextBox? 如何使用C#和SQL将日期添加到列类型为“日期/类型”的MS Access数据库中? - How do I add a date using C# & SQL to an MS Access database with column type 'Date/Type'? 如何在SQLite中检查数据库是否存在C# - How do I check in SQLite whether a database exists C# 如何检查数据库中是否已经存在用户名 - How do I check if a user name already exists in Database 如何检查记录是否已存在于SQL数据库中? - How do I check if the record already exists in SQL database? 如何检查数据库表中是否存在用户输入? - How do I check if the user's input exists in the database table? 如何检查用户名是否存在于数据库的特定列中 - How to check if Username exists in specific column of database 如何在C#中检查数据库中是否存在表(ACCESS或SQL) - how can i check whether a table exists in the database (ACCESS or SQL) in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM