简体   繁体   English

Linq to Sql-创建数据库以及如何从中查询?

[英]Linq to Sql - Create DB and How to query from it?

I create a table using Linq 我使用Linq创建表

PODataContext db = new PODataContext();

String query = "create table myTable(text varchar(20) primary key)";

db.ExecuteCommand(query);

Then I also inserted data to it. 然后我还向其中插入了数据。

String insQuery = "insert into myTable values('some text')";

db.ExecuteCommand(insQuery);

My problem is how do I get data from that table ? 我的问题是如何从该表中获取数据?

Thank You yohan 谢谢你yohan

var query =from  c in new PODataContext().myTable
            select c;

UPDATED 更新

LINQ to SQL without using the Object Relational Designer LINQ to SQL,无需使用对象关系设计器

Create a class named MyTable and decorate with Table attribute. 创建一个名为MyTable的类,并使用Table属性进行装饰。

[Table(Name="myTable")]
public class MyTable

{
    [Column]
    public string Text 

}

Then create following class 然后创建以下课程

public class PODataContext : DataContext
  {

   public Table<MyTable> myTables;

    public PODataContext(string connection): base(connection)

    {

    }

}

Now query your table as follows 现在如下查询您的表

var query =from  c in new PODataContext().myTable
            select c;

To do a query to the database table you don't need to write string commands and call the ExecuteQuery method - it is only for specific database tasks that you can't do with LINQ. 要查询数据库表,您无需编写字符串命令并调用ExecuteQuery方法-仅适用于LINQ无法执行的特定数据库任务。 What you can do with LINQ is write queries like: LINQ可以做的是写查询,例如:

var query = from c in db.myTable
            where c.Id > 5
            select c;

foreach ( var c in query )
    Console.WriteLine( c );

This code take all entities from myTable that have the Id greater than 5 ( SELECT * FROM myTable WHERE Id > 5 in SQL ) and write them all to the console. 此代码从myTable中获取ID大于5的所有实体( SELECT * FROM myTable WHERE Id > 5 in SQL ),并将它们全部写入控制台。

But to make it work you need to create right DataContext for your database. 但是要使其正常工作,您需要为数据库创建正确的DataContext The simpliest way to do it in Visual Studio is to Add New Item... -> LINQ to SQL Classes . 在Visual Studio中,最简单的方法是将Add New Item... > LINQ to SQL Classes Add New Item... LINQ to SQL Classes Then you need to connect to your database in Server Explorer , expand Tables and drag needed tables to designer window. 然后,您需要在Server Explorer连接到数据库,展开Tables并将所需的表拖到设计器窗口中。 Visual Studio automatically generates new class derived from DataContext that usefull for you database (for example has properties like myTable ). Visual Studio自动生成从DataContext派生的新类,该类对您的数据库myTable (例如,具有诸如myTable类的myTable )。

MSDN page for LINQ to SQL LINQ to SQL MSDN页面

If you are going to change (or create!) the database schema at runtime, note that LinqToSQL cannot update it's database representation at runtime as well. 如果要在运行时更改(或创建!)数据库模式,请注意,LinqToSQL也无法在运行时更新其数据库表示形式。 It can only be updated by Drag-And-Drop in Visual Studio or using SQLMetal via command line (a rather obscure method) and recompiling your application. 它只能通过Visual Studio中的拖放操作或通过命令行(相当模糊的方法)使用SQLMetal并重新编译应用程序来更新。

However, you could create a well-defined database schema with the ExecuteQuery at runtime and use the same existing database schema on your development machine to generate your LinqToSQL DataContext via drag and drop as usual. 但是,您可以在运行时使用ExecuteQuery创建一个定义良好的数据库架构,并像往常一样通过拖放操作在开发计算机上使用相同的现有数据库架构来生成LinqToSQL DataContext。

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

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