C# Linq to SQL DataContext:如何加载数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14262048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:13:45  来源:igfitidea点击:

Linq to SQL DataContext: how to load data?

c#asp.netlinq-to-sqldatacontext

提问by Ivan

(I'm completely new to Linq to SQL) I am creating a web app that works very closely with a database, I'm looking for the quickest and connection time efficient model and believing Linq to SQL to be this. I'm using C#/.Net4/Visual Studio 2010

(我对 Linq to SQL 完全陌生)我正在创建一个与数据库密切配合的 Web 应用程序,我正在寻找最快且连接时间高效的模型,并相信 Linq to SQL 就是这样。我正在使用 C#/.Net4/Visual Studio 2010

For simplicity sake, I have a web .aspx page containing a number of asp Text Boxes. I want to give their Text values from SQL data via Linq to SQL object. I also have a file called DataClasses.dbml with a a table added in the design view. The code I have so far in my web page code-behind is:

为简单起见,我有一个包含许多 asp 文本框的 web .aspx 页面。我想通过 Linq 将 SQL 数据中的文本值提供给 SQL 对象。我还有一个名为 DataClasses.dbml 的文件,其中在设计视图中添加了一个表。到目前为止,我的网页代码隐藏中的代码是:

DataClassesDataContext db = new DataClassesDataContext(getConnectionString);

var table = from t in db.MyTable
            where t.PK == 2
            select new { t.col1, t.col2, t.col3};

db.Connection.Open();
db.  // What's the best way of loading the object?
db.Connection.Close();

How do I then access the column values? Or do I databind it to a datatable? If so, how?

然后我如何访问列值?或者我是否将它数据绑定到数据表?如果是这样,如何?

myTextBox1.Text = table.???col1.value;

回答by arunlalam

Use

myTextBox1.Text = table.FirstOrDefault().col1.ToString();

回答by Dennis Traub

You don't need to open or close the connection. LinqToSql abstracts that away for you.

您不需要打开或关闭连接。LinqToSql 为您抽象了它。

Once you created the query, you can execute it and retrieve the row as an object using SingleOrDefault().

创建查询后,您可以执行它并使用SingleOrDefault().

using (var db = new DataClassesDataContext(getConnectionString))
{
    var query = from t in db.MyTable
                where t.PK == 2
                select new { t.col1, t.col2, t.col3};
    var myObject = query.SingleOrDefault();
}

You can also simplify this operation by using the lambda notation:

您还可以使用 lambda 表示法简化此操作:

using (var db = new DataClassesDataContext(getConnectionString))
{
    var myObject = db.MyTable.SingleOrDefault(t => t.PK == 2 )
}

To access the object you can directly access the columns since they have been mapped to the corresponding properties:

要访问对象,您可以直接访问列,因为它们已映射到相应的属性:

myTextBox1.Text = myObject.col1;

回答by tukaef

Common way is to call method that will execute query (ToArray, ToList, First, Single, etc..) or enumerate it in foreach.

常见的方法是调用将执行查询的方法(ToArray、ToList、First、Single 等)或在 foreach 中枚举它。

For example:

例如:

var query = from t in db.MyTable
            where t.PK == 2
            select new { t.col1, t.col2, t.col3};

var result = query.ToArray(); // now it contains array of resulting objects

// or enumerate
foreach (var obj in query)
{
    // do stuff with obj
}