In LINQPad can you access SYSOBJECTS using LINQ?
In LINQPad is there any way to access either the SYSOBJECTS table or the various INFORMATION_SCHEMA.xxx views using LINQ?
I spend a lot of time searchin开发者_运维百科g through our huge company database for partial names as there are too many tables and Stored Procedures to remember the names of them all.
I know I can enter and run SQL in LINQPad but I would like to do this in LINQ instead of SQL as LINQ is more fun :)
Thanks
Xanthalas
Yes you can.
All you have to do is include system views and SPs in selected connection and use LINQ like following:
sys.Sysobjects.Where(sp => sp.Xtype == "P") // returns SPs
sys.Sysobjects.Where(t => t.Xtype == "U") // returns Tables
or using sys.Views directly [example returns all tables with columns containing string "person" ]:
sys.Tables.Join(sys.Columns,
t => t.Object_id,
c => c.Object_id,
(t, c) => new { t, c })
.Where(x => x.c.Name.Contains("person"))
.Select(x => new { ObjName = x.t.Name,
ChildName = x.c.Name } )
.Distinct()
You can also embed SQL into your LINQ statements like so:
void Main()
{
var matches = this.ExecuteQuery<SysObject>("SELECT name, type_desc AS "
+ "TypeDesc FROM [sys].[objects]");
foreach(var match in matches)
Console.WriteLine("{0,-30}{1}", (match.Name + ":"), match.TypeDesc);
}
// Define other methods and classes here
class SysObject
{
public string Name;
public string TypeDesc;
// etc...
}
By default LinqPad doesn't use a monospaced font for results, but you can easily change it by pasting the following bit of css into "Edit -> Preferences -> Results -> Launch Editor"
body { font-family: Consolas, monospace; }
create a new table with the contents of SYSOBJECTS and then search within the new table
select * into SYSOBJECTS_COPY from SYS.OBJECTS
from o in SYSOBJECTS_COPY.AsEnumerable()
where Regex.IsMatch( d.Name, "partialName", RegexOptions.IgnoreCase )
select o
This code also returns the object definitions and lets you search within the definition if you want.
void Main()
{
var matches = FetchObjects(true);
var searchTerm = "tblName"; //<--Change this to filter for what you are looking for
bool searchName = true; //search the object name
bool searchDef = false; //search inside object definition (ie the stored procedure definition)
TypeDescs typeDesc = TypeDescs.Any; //specify whether you want to limit your search to tables or stored procedures
matches
.Where(x=> (
(searchName && x.Name.Contains(searchTerm))
|| (searchDef && (x.ObjectDefinition!=null && x.ObjectDefinition.Contains(searchTerm))) )
&& (typeDesc==TypeDescs.Any || x.TypeDesc == typeDesc.ToString())
)
.Select(x=> new {x}).Dump();
}
IEnumerable<SysObject> FetchObjects(bool includeDefinitions){
return this.ExecuteQuery<SysObject>("SELECT Name=convert(varchar(30), name), type_desc AS "
+ " TypeDesc "
+ string.Format(", ObjectDefinition={0}", (includeDefinitions)?"OBJECT_DEFINITION (OBJECT_ID(name))":"NULL")
+ " FROM [sys].[objects]");
}
enum TypeDescs {Any, SQL_STORED_PROCEDURE, USER_TABLE}
class SysObject
{
public string Name;
public string TypeDesc;
public string ObjectDefinition;
}
from d in Databases
select d
when database connection in LINQPad points to master database.
In addition to @Nick's answer, here is a snippet that generates table information in markdown and opens it in VS Code.
精彩评论