Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何返回所有列的名称?_C#_Sqlite_Pragma - Fatal编程技术网

C# 如何返回所有列的名称?

C# 如何返回所有列的名称?,c#,sqlite,pragma,C#,Sqlite,Pragma,搜索时,我发现PRAGMA是解决问题的可能方法,但它只返回每列的索引。有没有其他方法可以返回所有列的名称 我认为使用For遍历我的列索引并返回它们的名称会很好,但是我不知道它的语法是什么,也不知道stop条件 void FillColumnList() { try { string check = "SELECT * FROM PRAGMA table_info(Produtos)";

搜索时,我发现PRAGMA是解决问题的可能方法,但它只返回每列的索引。有没有其他方法可以返回所有列的名称

我认为使用For遍历我的列索引并返回它们的名称会很好,但是我不知道它的语法是什么,也不知道stop条件

void FillColumnList()
        {
            try
            {
                string check = "SELECT * FROM PRAGMA table_info(Produtos)";
                sqlCon.Open();
                SQLiteCommand tst2 = new SQLiteCommand(check, sqlCon);
                SQLiteDataReader rdr2 = tst2.ExecuteReader();                
                if (rdr2.HasRows)
                {
                    while (rdr2.Read())
                    {
                        string columns = rdr2[0].ToString();
                        Columns.Add(columns);
                    }
                    sqlCon.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }            
        }

此代码应返回全局变量列表列,并用“Produtos”表中每列的名称填充这些列。取而代之的是,我的DataReader“rdr2”在HasRows中返回false,即使我的表“Produtos”中有列和数据,也可以使用连接的
GetSchema
方法检索列信息。我正在使用以下代码插入我自己的类
TableColumn
中未显示的信息:

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

using (DataTable columns = conn.GetSchema("Columns", restrictions)) {
    int nameIndex = columns.Columns.IndexOf("COLUMN_NAME");
    int ordinalPosIndex = columns.Columns.IndexOf("ORDINAL_POSITION");
    int isNullableIndex = columns.Columns.IndexOf("IS_NULLABLE");
    int maxLengthIndex = columns.Columns.IndexOf("CHARACTER_MAXIMUM_LENGTH");
    int dataTypeIndex = columns.Columns.IndexOf("DATA_TYPE");
    int isPrimaryKeyIndex = columns.Columns.IndexOf("PRIMARY_KEY");
    int hasDefaultIndex = columns.Columns.IndexOf("COLUMN_HASDEFAULT");
    int defaultValueIndex = columns.Columns.IndexOf("COLUMN_DEFAULT");

    foreach (DataRow row in columns.Rows) {
        var col = new TableColumn {
            ColumnName = (string)row[nameIndex]
        };
        try {
            col.ColumnNameForMapping = prepareColumnNameForMapping(col.ColumnName);
        } catch (Exception ex) {
            throw new UnimatrixExecutionException("Error in delegate 'prepareColumnNameForMapping'", ex);
        }
        col.ColumnOrdinalPosition = (int)row[ordinalPosIndex];
        col.ColumnAllowsDBNull = (bool)row[isNullableIndex];
        col.ColumnMaxLength = (int)row[maxLengthIndex];

        string explicitDataType = ((string)row[dataTypeIndex]).ToLowerInvariant();
        col.ColumnDbType = GetColumnDbType(explicitDataType);
        col.ColumnIsPrimaryKey = (bool)row[isPrimaryKeyIndex];
        col.ColumnIsIdentity = explicitDataType == "integer" && col.ColumnIsPrimaryKey;
        col.ColumnIsReadOnly = col.ColumnIsIdentity;

        if ((bool)row[hasDefaultIndex]) {
            col.ColumnDefaultValue = GetDefaultValue(col.ColumnDbType, (string)row[defaultValueIndex]);
            if (col.ColumnDefaultValue == null) { // Default value could not be determined. Probably expression.
                col.AutoAction = ColumnAction.RetrieveAfterInsert;
            }
        }
        tableSchema.ColumnSchema.Add(col);
    }
}
如果只需要列名,就可以大大简化此代码