简体   繁体   English

ADO.NET适当的INSERT数据

[英]ADO.NET proper INSERT of data

I am using a SQL CE 3.5 database in my C# project. 我在我的C#项目中使用SQL CE 3.5数据库。

When I am adding new data to a table, let's say a simple row, I feel like I am doing it totally wrong. 当我向表中添加新数据时,让我们说一个简单的行,我觉得我完全错了。 What I am doing is something like this: 我在做什么是这样的:

const string commandStr = "INSERT INTO tableName (size, x, y) VALUES(@size, @x, @y)";
using (SqlCeCommand command = new SqlCeCommand(commandStr, _connection))
{
    command.Parameters.AddWithValue("@size", size);
    command.Parameters.AddWithValue("@x", x);
    command.Parameters.AddWithValue("@y", y);
    int affectedRows = command.ExecuteNonQuery();
}

Of course, my actual table that I am using in my project has a lot more collumns. 当然,我在我的项目中使用的实际表有更多的列。 Obviously, this is a lot of typing. 显然,这是很多打字。 This brings me to the point where I am thinking: "I am a .NET devoloper. There must be something that feels a lot more naturally." 这让我想到了我正在思考的问题:“我是一个.NET devoloper。必定会有更自然的感觉。”

So my questions are: Is there a nicer way to do this? 所以我的问题是:有更好的方法吗? If so, how? 如果是这样,怎么样?

Sometimes there are edge cases where doing it your way is required. 有时会有边缘情况需要按照您的方式进行。 However, for a full, verbose, ".NET developer" feel, try Entity Framework. 但是,对于一个完整,冗长的“.NET开发人员”的感觉,请尝试实体框架。 It is awesome. 太棒了。

Entity Framework is so nice for this in my opinion. 在我看来, 实体框架对此非常好。 I really enjoy using it. 我真的很喜欢使用它。

Yes. 是。 It's called Linq :) http://msdn.microsoft.com/en-us/library/bb308959.aspx 它被称为Linq :) http://msdn.microsoft.com/en-us/library/bb308959.aspx

Linq 2 SQL or Linq 2 Entities, depending on your specific needs. Linq 2 SQL或Linq 2实体,具体取决于您的特定需求。 I find it more natural than using raw ado.net and I bet you won't feel any real performance problems. 我发现它比使用原始ado.net更自然,我打赌你不会感觉到任何真正的性能问题。

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

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