views:

53

answers:

1

Hi.. my linq knowledge is ok. i need only a loop or any other method to accomplish ado.net method :(LOOK PLEASE BOLD CODES)


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;
}
}


i need:


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

how can i do that?

can you give clear sample:

like :


using ( var contex = new DataclassContext())
foreach (string ky in ld.Keys)
{
contex .Parameters.AddWithValue(ky, ld[ky]);
}
Can i make it linq to sql.

contex.SubmitChanges();

+1  A: 

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

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.

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.

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

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.

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:

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);
    }
}

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.

Sam
can we do that?using ( var myEntity = new EntityObject())foreach (string ky in ld.Keys){myEntity .Parameters.AddWithValue(ky, ld[ky]);}i nee it i don't want to write Column a ,b,c, d, .. . .. . z
Phsika
I've updated the answer, showing how you could use reflection to achieve this. However, I think you should rethink why you are passing an array of key/value pairs between your methods, rather than passing the LINQ->SQL entity objects directly.
Sam
thaks alot i will make it . than i post result thaks toooo much
Phsika
But your codes not running...
Phsika