如何使用C#中的查询获得SQLite表格架构

发布于 2025-02-03 06:13:43 字数 527 浏览 3 评论 0原文

我一直在高低搜索,使用许多方法从sqlite的表中获取表格架。请原谅我是否已将其发布到其他地方。

当前查询是,

SELECT TOP (0) * FROM 'TableName'

但这一无所获。

SELECT * FROM 'TableName'

如果桌子为空,也不会返回。

我还使用了sqliteconnection.getschema(),但它不会返回所讨论的表。它返回带有其他列的表。

DataTable dtReturnTable = new DataTable();
sqlitecon.Open();
string[] sParameters = new string[4];
sParameters[2] = sTableName;
dtReturnTable = sqlitecon.GetSchema("Columns", sParameters);
sqlitecon.Close();

I have been searching high and low, using many methods to get a table schema from a table in sqlite. Please forgive me if this has been posted somewhere else.

Current query is

SELECT TOP (0) * FROM 'TableName'

but this returns nothing.

SELECT * FROM 'TableName'

also returns nothing if the table is empty.

I also used SQLiteConnection.GetSchema() but it doesn't return the table in question; it returns a table with other columns.

DataTable dtReturnTable = new DataTable();
sqlitecon.Open();
string[] sParameters = new string[4];
sParameters[2] = sTableName;
dtReturnTable = sqlitecon.GetSchema("Columns", sParameters);
sqlitecon.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2025-02-10 06:13:43

所以我做的是

private DataTable GetSQLiteTableWithQuery(string sQuery)
        {
            SQLiteConnection sqlconlocal = new SQLiteConnection(sSQLITEconnstring);
            DataTable dtReturnTable = new DataTable();
            SQLiteCommand sqliteCommand = new SQLiteCommand(sQuery, sqlconlocal);
            using (sqlconlocal)
            {
                try
                {
                    sqlconlocal.Open();
                    SQLiteDataReader sqliteReader = sqliteCommand.ExecuteReader();
                    if (sqliteReader.HasRows)
                    {
                        for (int i = 0; i < sqliteReader.FieldCount; i++)
                        {
                            dtReturnTable.Columns.Add(new DataColumn(sqliteReader.GetName(i)));
                        }
                    }
                    int j = 0;
                    while (sqliteReader.Read())
                    {
                        DataRow dtRow = dtReturnTable.NewRow();
                        dtReturnTable.Rows.Add(dtRow);
                        for (int i = 0; i < sqliteReader.FieldCount; i++)
                        {
                            dtReturnTable.Rows[j][i] = (sqliteReader.GetValue(i));
                        }
                        j++;
                    }
                    sqlconlocal.Close();
                }
                catch (SQLiteException e)
                {
                    sqlconlocal.Close();
                }
            }
            return dtReturnTable;
        }

public DataTable GetSQLiteSchema(string sTableName)
        {
            string sQuery = "PRAGMA table_info('" + sTableName + "')";
            DataTable dtWorking = GetSqLiteTableBasedOnQuery(sQuery);
            DataTable dtReturn = new DataTable();
            foreach (DataRow dr1 in dtWorking.Rows)
            {
                string sColName = dr1[1].ToString();
                string sColtype = dr1[2].ToString();
                if (sColtype.ToLower().Contains("nvarchar"))
                {
                    dtReturn.Columns.Add(sColName, typeof(string));
                }
                else if (sColtype.ToLower().Contains("decimal"))
                {
                    dtReturn.Columns.Add(sColName, typeof(decimal));
                }
                else if (sColtype.ToLower().Contains("int"))
                {
                    dtReturn.Columns.Add(sColName, typeof(int));
                }
                else if (sColtype.ToLower().Contains("date"))
                {
                    dtReturn.Columns.Add(sColName, typeof(DateTime));
                }
                else if (sColtype.ToLower().Contains("bit"))
                {
                    dtReturn.Columns.Add(sColName, typeof(bool));
                }
            }
            return dtReturn;
        }

So what I did was this

private DataTable GetSQLiteTableWithQuery(string sQuery)
        {
            SQLiteConnection sqlconlocal = new SQLiteConnection(sSQLITEconnstring);
            DataTable dtReturnTable = new DataTable();
            SQLiteCommand sqliteCommand = new SQLiteCommand(sQuery, sqlconlocal);
            using (sqlconlocal)
            {
                try
                {
                    sqlconlocal.Open();
                    SQLiteDataReader sqliteReader = sqliteCommand.ExecuteReader();
                    if (sqliteReader.HasRows)
                    {
                        for (int i = 0; i < sqliteReader.FieldCount; i++)
                        {
                            dtReturnTable.Columns.Add(new DataColumn(sqliteReader.GetName(i)));
                        }
                    }
                    int j = 0;
                    while (sqliteReader.Read())
                    {
                        DataRow dtRow = dtReturnTable.NewRow();
                        dtReturnTable.Rows.Add(dtRow);
                        for (int i = 0; i < sqliteReader.FieldCount; i++)
                        {
                            dtReturnTable.Rows[j][i] = (sqliteReader.GetValue(i));
                        }
                        j++;
                    }
                    sqlconlocal.Close();
                }
                catch (SQLiteException e)
                {
                    sqlconlocal.Close();
                }
            }
            return dtReturnTable;
        }

public DataTable GetSQLiteSchema(string sTableName)
        {
            string sQuery = "PRAGMA table_info('" + sTableName + "')";
            DataTable dtWorking = GetSqLiteTableBasedOnQuery(sQuery);
            DataTable dtReturn = new DataTable();
            foreach (DataRow dr1 in dtWorking.Rows)
            {
                string sColName = dr1[1].ToString();
                string sColtype = dr1[2].ToString();
                if (sColtype.ToLower().Contains("nvarchar"))
                {
                    dtReturn.Columns.Add(sColName, typeof(string));
                }
                else if (sColtype.ToLower().Contains("decimal"))
                {
                    dtReturn.Columns.Add(sColName, typeof(decimal));
                }
                else if (sColtype.ToLower().Contains("int"))
                {
                    dtReturn.Columns.Add(sColName, typeof(int));
                }
                else if (sColtype.ToLower().Contains("date"))
                {
                    dtReturn.Columns.Add(sColName, typeof(DateTime));
                }
                else if (sColtype.ToLower().Contains("bit"))
                {
                    dtReturn.Columns.Add(sColName, typeof(bool));
                }
            }
            return dtReturn;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文