检查是否在OleDb的表中存在的列 [英] Check if column exists in OleDb table

查看:129
本文介绍了检查是否在OleDb的表中存在的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个列上存在,如果没有,添加它。我试过一对夫妇的解决方案,包括本<中/ ,一>,但语法不是Access数据库正确

I am trying to check if a column exists and if not, add it. I've tried a couple of solutions including this, but the syntax isn't correct for Access db.

这是我迄今为止:

    public void Update(string task, string dbPath, string tableName = "Frames")
    {
        OleDbConnection db = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source=" + dbPath);
        db.Open();

        OleDbCommand command = db.CreateCommand();
        command.CommandText = "COL_LENGTH('Frames','SetNumber')";
        Debug.WriteLine(command.ExecuteReader());




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

        DataTable dtColumns = db.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, restrictions);

        foreach (DataColumn column in dtColumns.Columns)
        {
            Debug.WriteLine(column.ColumnName);
        }*/

    }



我:使用GetOleDbSchemaTable也尝试但它没有返回右表什么的。
我缺少什么?

I also tried using GetOleDbSchemaTable but it isn't returning the right table or something. What am I missing?

推荐答案

要检查是否列在数据表中存在,您可以使用GetSchema方法该OleDbConnection的

To check if a column exist in a datatable you could use the GetSchema method of the OleDbConnection

public void Update(string task, string dbPath, string colName, string tableName = "Frames") 
{ 
    using(OleDbConnection db = new OleDbConnection("........"))
    {
        db.Open(); 
        var schema = db.GetSchema("COLUMNS"); 
        var col = schema.Select("TABLE_NAME='" + tableName + 
                   " AND COLUMN_NAME='" + colName + "'" 

        if(col.Length > 0)
           // Column exist
        else
           // Column doesn't exist
} 

这篇关于检查是否在OleDb的表中存在的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆