且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法在 SQL Server CE 中一次搜索所有表的字段?

更新时间:2023-02-25 14:34:18

我已经改编了 Syn123 的答案中的 SQL,这是我目前所拥有的:

I've adapted the SQL from Syn123's answer and here is what I have so far:

SELECT c.TABLE_NAME, c.COLUMN_NAME
FROM   INFORMATION_SCHEMA.COLUMNS AS c 
       INNER JOIN INFORMATION_SCHEMA.Tables AS t ON t.TABLE_NAME = c.TABLE_NAME
WHERE  (c.DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'text', 'ntext')) AND (t.TABLE_TYPE = 'TABLE')

我遇到的问题是我无法使用 SQL CE 进行子查询,因此无法从结果集中进行选择.如果您有权访问您的 .MDF 数据库文件,您可以编写一个小型控制台应用程序来使用上述 SQL 返回的集合搜索特定关键字.您需要一种创建动态 SQL 的方法,而 SQL CE 不支持 EXEC,因此这很困难,而且很可能本身不可能.

The problem I'm having is that I can't do subqueries with SQL CE and thus can't select from a result set. If you have access to your .MDF database file, you can write a small console app to search for a particular keyword using the set returned by the above SQL. You need a way to create dynamic SQL and SQL CE doesn't support EXEC so this is difficult and most likely not possible by itself.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLCESearch
{
    class Program
    {
        static void Main(string[] args)
        {
            SearchText("Nancy");
            Console.ReadKey();
        }

        private static void SearchText(string searchText)
        {
            string connStr = "Data Source=Northwind40.sdf;Persist Security Info=False;";
            DataTable dt = new DataTable();
            try
            {
                string sql = "SELECT c.TABLE_NAME, c.COLUMN_NAME ";
                sql += "FROM   INFORMATION_SCHEMA.COLUMNS AS c ";
                sql += "INNER JOIN INFORMATION_SCHEMA.Tables AS t ON t.TABLE_NAME = c.TABLE_NAME ";
                sql += "WHERE  (c.DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'text', 'ntext')) AND (t.TABLE_TYPE = 'TABLE') ";

                SqlCeDataAdapter da = new SqlCeDataAdapter(sql, connStr);
                da.Fill(dt);

                foreach (DataRow dr in dt.Rows)
                {
                    string dynSQL = "SELECT [" + dr["COLUMN_NAME"] + "]";
                    dynSQL += " FROM [" + dr["TABLE_NAME"] + "]";
                    dynSQL += " WHERE [" + dr["COLUMN_NAME"] + "] LIKE '%" + searchText + "%'";

                    DataTable result = new DataTable();
                    da = new SqlCeDataAdapter(dynSQL, connStr);
                    da.Fill(result);
                    foreach (DataRow r in result.Rows)
                    {
                        Console.WriteLine("Table Name: " + dr["TABLE_NAME"]);
                        Console.WriteLine("Column Name: " + dr["COLUMN_NAME"]);
                        Console.WriteLine("Value: " + r[0]);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }


        }
    }
}

有一个非常快速和肮脏的控制台应用程序,它将打印出包含文本值的任何表/列.您可以对其进行调整以满足您的需求.我希望这会有所帮助.

There's a really quick and dirty console application that will print out any table/column that contains a value of text. You can adapt it to fit your needs. I hope this helps.