Reading / writing to entity field using name stored in variable - c#

I have a winforms application which uses Linq to SQL. One of my database tables has a number of fields like this...
Area_1
Area_2
Area_3...
I need to be able to access these fields dynamically at runtime. I have a number of wired up buttons on my form which correspond to the different areas, so I created a string like this...
String sField = String.Format("Area_{0}", myAreaNumber);
And need to be able to read / write to the specific field using this variable. At this stage, I have already grabbed the entity object I need to work with. How can I go about using the 'sField' variable to read / write to a field?

You can run raw SQL directly on database using SqlQuery method on DbSet:
context.YourDbSet.SqlQuery("query string")

Related

Building an SQL in list using C# variable

I have an SQL where clause: -
where table1.resource in #ListOfRes
I'm trying to add to the SQL command like this:-
command.Parameters.AddWithValue("#ListOfRes", "'1','2'");
The result I'm looking for is: -
where table1.resource in ('1','2')
But I can't seem to build a string this way, is there a different data type I need to be using?
Nope, Not working. This is not how SQL works, regardless what you do.
IN (#variable) takes the content of the variable as ONE ELEMENT. There is no way to put multiple elements in it. Live with it. You need one variable for every element, or another approach (temp file, then using a join etc.)..
This simply is not a supported approach.

C# translate database data using resx file

In my project (I am using azure storage) I have some data that I want to translate. I have the resource system in place for translations. I have a table in cloud which has name property. I want to translate it somehow.
One option is to create all the entries in database for each language which I don't prefer as it would create a lot entries along with the name.
Is there a smart way to use the resx mechanism I have in place?
So the table has multiple properties and one is name. Name could be anything like Mud, rock etc. Now I want to translate Mud into different language. Something like Texts.Mud would return me the correct value.
But lets say I get data like this
var data = some query;
string translatedName = Texts.data[0].name; // this won't work
You should instead add more columns in the database, each for a different language and select the column based on the user language.
Other solution is to have a transaltion mechanism (a custom class for example), where you pass the original database result (say data[0].name) to a query and it returns the translated value for you.

Reading a string formatted like XML

I have a string that is written out like an XML file. An example would look like this:
string = <Employees><EmployeeId>1</EmployeeId>< ... ></Employees>
I am saving this in a table because I wanted to audit changes, but I didn't want to have multiple tables for different audits. This is because it would record changes to things other than employees. So using an XML style string in the database seemed like a good suggestion.
Now to the real business. I want to check to make sure that there were actually changes to the employee because one could go into the edit page, change nothing, and click save. As of right now, the data would write to the DB and just clutter it up with non-changed data.
I'd like to be able to check if the XML styled string that is going to be saved is on the database, so if <employees><employeeid>###</employeeid> == "changes" and then see if the whole string equals the other. Basically, check the employeeId first because that won't change, and then check the string as a whole to see if there is any difference. I would have just checked the first n numbers, but the id number could have a length of 1 to 3.
Also, because it is styled as XML, is there an easy way to convert it to read it like an XML file and check that way?
Storing arbitrary data in a column is a form of denormalization. You can't really do much with it at a database level. However, SQL Server does have an XML column type. Entity Framework doesn't support mapping to/from an XML column, so it will simply treat your XML as a standard string. With this column type, though, you can write actual SQL queries against your XML using XPath expressions.
Your best bet, then, is to type your column as XML, and then write a stored procedure that performs the query you need. You can then utilize this stored procedure with Entity Framework.
For more information on the XML column type see: https://msdn.microsoft.com/en-us/library/ms190798(SQL.90).aspx

How to pass a string array into a stored procedure in SQL Server from c# via ADO.NET entity model

I am currently developing an application in c# where I have a string array NameArray and recorded the number of values in the array as count. I need to pass this to my sql server database where I will store each of the value of the array as individual records(names) into a Table. How do I write a stored procedure for doing such a operation.
Furthermore the name of the entity connection string is MyEntities and I have created its object as Entityobj.
The code I am trying to execute in c# is something like this
public void Method1(string[] NameArray, int count)
{
Entityobj.CallSproc(NameArray,count);// Here I am passing the values to stored procedure
}
How do I make the stored procedure receive the values and store them as individual records?
Anyways I worked out the problem myself with the help of using this link
http://www.c-sharpcorner.com/UploadFile/78607b/using-table-valued-parameters-in-entity-framework/
The link shows how to pass simple values to Table value parameters using Entity object. I edited the code to pass an by adding each value of the array into a single row(through iteration). I don't know whether this is a efficient way to do it but it serves my purpose well and code is working pretty fine.
Also there was a warning in my code as Entity Framework does not support table as datatype(table valued parameters) but the code worked fine anyway.

Converting logic of DateTime.FromBinary method in TSQL query

I have a table that contain column with VARBINARY(MAX) data type. That column represents different values for different types in my c# DB layer class. It can be: int, string and datetime. Now I need to convert that one column into three by it's type. So values with int type go to new column ObjectIntValue and so on for every new column.
But I have a problems with transmitting data to datetime column, because the old column contains datetime value as a long received from C# DateTime.ToBinary method while data saving.
I should make that in TSQL and can't using .NET for convert that value in new column. Have you any ideas?
Thanks for any advice!
Using CLR in T_SQl
Basically you use Create Assembly to register the dll with your function(s) in it,
Then create a user defined function to call it, then you can use it.
There's several rules depending on what you want to do, but as basically you only want DateTime.FromBinary(), shouldn't be too hard to figure out.
Never done it myself, but these guys seem to know what they are talking about
CLR in TSQL tutorial
This is a one off convert right? Your response to #schglurps is a bit of a concern.
If I get you there would have to be break in your update script, ie the one you have woukld work up to when you implement this chnage, then you's have a one off procedure for this manouevre, then you would be updating from a new version.
If you want to validate it, just check for the existnec or non-existance of the new columns.
Other option would be to write a wee application that filled in the new columns from the old one and invoke it. Ugh...
If this isn't one off and you want to keep and maintain the old column, then you have problems.

Categories

Resources