循环结构使用addwithValue持久化数据 [英] Loop structure to perist data using addwithValue

查看:46
本文介绍了循环结构使用addwithValue持久化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的LINQ知识还可以.我需要一个循环或另一种方法来完成ADO.NET方法(请参见foreach循环)

My LINQ knowledge is ok. I need a loop or another method to accomplish ADO.NET method (see foreach loop)

public bool AccessProcess(string sp, ListDictionary ld, CommandType cmdType)
{
    SqlConnection con = new SqlConnection(
        WebConfigurationManager.ConnectionStrings["conn"].ToString());
    SqlCommand cmd = new SqlCommand(sp, con);
    try
    {
        con.Open();
        cmd.CommandType = cmdType;
        foreach (string ky in ld.Keys)
        {
            cmd.Parameters.AddWithValue(ky, ld[ky]);
        }
        cmd.ExecuteNonQuery();
    }
    finally
    {
        con.Dispose();
        cmd.Dispose();
    }
    return true;
}

我需要:

foreach (string ky in ld.Keys)
    yourObject.SetPropertyValue(ky, ld[ky]);

我该怎么做?我可以使用LINQ-to-SQL吗?

How can I do that? Can I use LINQ-to-SQL?

您能提供清晰的示例吗

using (var contex = new DataclassContext())
foreach (string ky in ld.Keys)
{
    contex.Parameters.AddWithValue(ky, ld[ky]);
}
contex.SubmitChanges();

推荐答案

Linq to SQL将数据类(通常称为实体")映射到SQL数据库表.

Linq to SQL maps data classes (often called an 'Entities') to a SQL database table.

因此,首先,您需要使用Linq-> SQL设计器为您生成这些数据类,以及一个DataContext对象,该对象将这些类与数据库进行映射.

So, first you need to use the Linq->SQL designer to generate these data classes for you, and a DataContext object that will map theses classes to and from the database.

您通常在Visual Studio的Linq到SQL设计器UI中定义此映射.通过该UI,您可以直观地编辑.dbml文件,并从本质上定义CLR实体对象和支持的SQL数据库表之间的映射.

You usually define this mapping in the Linq to SQL designer UI in Visual Studio. This UI let's you visually edit a .dbml file and essentially defines a mapping between your CLR entity objects and the backing SQL database table.

请参阅此MSDN文章,以获取有关如何添加的示例一个新的Linq to SQL类.

See this MSDN article for an example of how to add a new Linq to SQL class.

一旦定义了此类,就可以创建该类及其属性值的新实例.这些属性值映射到SQL数据库表中的列.然后,将该对象传递给LINQ to SQL生成的DataContext类,以向数据库中的表添加新行.

Once you have defined this class, you can create a new instance of this class and its property values. These property values are mapped to columns in the SQL database table. You then pass this object to the DataContext class generated by LINQ to SQL, to add a new row to the table in the database.

因此,您将执行以下操作:

So, you would do something like this:

For a database table with columns "ColumnA", "ColumnB" and "ColumnC"

var myEntity = new EntityObject { ColumnA = "valueA", ColumnB = "valueB", "ColumnC" = "valueC" };
DataContext.InsertOnSubmit(myEntity);
DataContext.SubmitChanges();

这会将具有指定列值的新行插入数据库.

This will insert a new row into the database with the column values specified.

现在,如果您不想手动编写代码以写入ColumnA,ColumnB等,则可以使用反射来写入实体上的适当属性:

Now if you don't want to manually write code to write to ColumnA, ColumnB, etc, then you could use reflection to write to the appropriate properties on the entity:

例如,使用实体实例"myEntity":

For example, with entity instance 'myEntity':

var properties = myEntity.GetType().GetProperties();

foreach (string ky in ld.Keys)
{
    var matchingProperty = properties.Where(p => p.Name.Equals(ky)).FirstOrDefault();
    if (matchingProperty != null)
    {
        matchingProperty.SetValue(myEntity, ld[ky], null);
    }
}

使用反射不能提供最佳性能,所以我会问为什么您甚至想将键和值的数组映射到linq-> sql实体,而不是直接在当​​前生成的代码中使用实体对象这些键/值对.

Using reflection doesn't give the best performance, so I would question why you even want to map an array of keys and values to a linq->sql entity, rather than using the entity object directly in the code that currently generates these key/value pairs.

这篇关于循环结构使用addwithValue持久化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆