繁体   English   中英

如何在Visual Studio 2017中使用“C#Interactive”窗口查询“数据连接”中的源代码

[英]How can I use, in Visual Studio 2017, the “C# Interactive” window to query a source in my “Data Connections”

我在“数据连接”(在“服务器资源管理器”视图中)连接到外部SQL服务器。 我可以右键单击我的SQL源并单击“新建查询”以使用SQL语句快速查找数据。

我想改用LINQ,我认为“C#Interactive”窗口是一个很好的快速方法。 我的问题是我不知道如何访问我的“开放”数据连接。 无法识别数据库的名称。

我通过创建一个类库来打开与EF数据模型的连接,将DLL导入C#交互式窗口,并对数据模型执行Linq语句,从而实现了这一点。

首先,创建类库,添加EF数据模型,并修改DbContext(实体)类以使用带有连接字符串的构造函数。 您需要这样做才能在c#interactive窗口中使用此库,因为如果不这样做,交互式窗口将查找带有连接字符串的app.config文件。

public partial class YourDBEntities : DbContext
{
    public YourDBEntities(string connectionString)
        : base(connectionString)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    ....
}

如果你的类库,添加一个带有静态方法的类来获取数据上下文:

public class AccessorClass
{
    public static YourDBEntities GetDataContext()
    {
        return new YourDBEntities("metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=\";data source=xxxxxxx;initial catalog=xxxxxxx;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework\";");
    }
}

编译类库,然后将DLL导入交互式窗口,并查询:

> #r "C:\Path...\bin\Debug\YourClassLibrary.dll"
> using YourClassLibrary;
> using (var ctx = AccessorClass.GetDataContext())
. {
.     Console.Write(ctx.Orders.Where(c => c.ProjectID == 309).Count().ToString());
. }

是的,您可以在解决方案资源管理器中右键单击主项目,然后单击“ 使用项目初始化Interacive” 这将构建您的项目并将所有dll导入到交互式窗口中。 然后你就可以开始刮擦了!

例如,使用Entity Framework,您需要站起来DbContext 输入类似......

> var context = new My.Namespace.MyDataContext("blah blah blah");

在我写“blah blah blah”的地方你需要添加你的连接字符串。 交互式控制台不知道您的.config文件,因此您需要提供连接字符串。

注意:为了能够执行此操作,请确保在数据上下文中具有nameOrConnectionString构造函数覆盖。

既然你有上下文就像通常查询上下文一样简单......

> context.Users.Where(u => u.IsActive).Select(u => u).ToList()

重要
请注意,我在查询结尾处留下了分号(;)。 这很重要,因为它告诉控制台输出查询/命令/ 代码行的值 如果你不这样做,什么都不会发生。

我提出的解决方案可能并不完全符合您的要求,但我认为这将有助于您找出所需的解决方案。 我做过类似的一种方法是创建一个DA库并在C# Interactive Window使用它。 以下是样本:

我有一个类库项目,MyProject.MyDA:

namespace MyDa
{
    public class CustomerDa
    {
        public DataTable LoadData(string sqlCommandText = "")
        {
            //do your try catch finally and all the good stuff
            var connString = @"Data Source=ServerName;Initial Catalog=AdventureWorks2014;Integrated Security=SSPI;";
            var conn = new SqlConnection(connString);
            SqlDataReader dataReader;
            //you could accept the command text as a parameter
            string sql = "select top 10 * FROM [AdventureWorks2014].[HumanResources].[Department]";
            var result = new DataTable("Department");
            conn.Open();
            SqlCommand command = new SqlCommand(sql, conn);
            dataReader = command.ExecuteReader();
            result.Load(dataReader);
            dataReader.Close();
            command.Dispose();
            conn.Close();
            //instead of a datatable, return your object
            return result;
        }
    }
}

建立你的DA项目,现在在C# Interactive ,你会这样做:

> #r "D:\blah\Blah\MyDa\bin\Debug\MyDa.dll"
> using MyDa;
> var a = new CustomerDa();
> var r = a.LoadData();
> r.Rows[0]
DataRow { HasErrors=false, ItemArray=object[4] { 1, "Engineering", "Research and Development", [4/30/2008 12:00:00 AM] }, RowError="", RowState=Unchanged, Table=[] }
> r.Rows.Count //you can do all the good LINQ stuff now on the result
10

你可以这样做,但我觉得这种流程需要比我想要的更多的工作和仪式,并且仍然是不完美的。 无论如何,这是实现您所寻找的一种方式。 如果您更喜欢使用LINQ查询,我还建议使用LinqPad

这个可以工作!

    SELECT 
    DB_NAME(dbid) as DBName, 
    COUNT(dbid) as NoOfConnections,
    loginame as LoginName
    FROM
    sys.sysprocesses
    WHERE 
    dbid > 0
    GROUP BY 
dbid, loginame

您可以在此链接中查找数据库中的打开连接数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM