In a EF6 context, I want to filter multiples entities by dynamic fields/attributes. I'm new to EF and my perspective is very corrupted by T-SQL and stored procedures and dynamic SQL queries.
For instance in a ERP environment, the user can filter by a code, and the system should return the:
Customer with CustomerID = code
Supplier with SupplierID = code
User with UserID = code
Orders with CustomerID/SupplierID = code
etc.
But can not only be a code, can multiples concepts to filter for: a name, a city, a date, ... and may all not apply to all the entities.
So since each entity has different attributes names to refer to that "code" concept, I've thought that the best solution is to use EntityCommand instead of LinQ.
And the code should look something like:
// Create a query that takes two parameters.
string eSqlCustomerQuery =
#"SELECT VALUE Contact FROM AdventureWorksEntities.Customer AS Customer";
string eSqlCustomerQuery =
#"SELECT VALUE Contact FROM AdventureWorksEntities.Customer AS Customer";
// Create a list of parameters
var param = new SortedList<string, EntityParameter>();
// for each clauses add a pamater and build the query command dynamically.
if(!code)
{
eSqlCustomerQuery += "WHERE Customer.CustomerID = #CODE";
eSqlSupplierQuery += "WHERE Supplier.SupplierID = #CODE";
//... more entities to
param["CODE"].ParameterName = "CODE";
param["CODE"].Value = code;
}
// more parameters here...
using (EntityConnection conn =
new EntityConnection("name=AdventureWorksEntities"))
{
conn.Open();
using (EntityCommand cmd = new EntityCommand(eSqlCustomerQuery, conn))
{
cmd.Parameters.Add(param["CODE"]);
cmd.Parameters.Add(param["DATE"]);
// more parameters here...
}
// the same for each query...
// ...
// run all the queries ...
// ...
// Etc.
conn.Close();
}
My questions are 3:
At the time I'm doing cmd = new EntityCommand(eSqlCustomerQuery, conn) can I use something like the System.Data.SqlClient.SqlCommandBuilder.DeriveParameters(cmd);?
Since this dynamic query it's so dynamic that it can be cached or have a reusable execution plan, how can it be improved?
Is it possible to do it with LinQ in a cleaner way?
Use LINQ like that:
//define base LINQ
Contracts = from R in AdventureWorks.Customer select R; //there is IQueryable, not actually materialized
//tune filters, no records will fetched
Result = Contracts;
if (code!=null) Result = Result.Where(_=>_.Code==code);
if (date!=null) Result = Result.Where(_=>_.Date==date);
//materialize records
Records = Result..Select(_=>_.Contract).ToArray();
Related
How can I use dynamic queries in C# ? From what I've searched its similiar to when we use SqlCommand with parameters to prevent sql injection(example below).
using (SQLiteConnection DB_CONNECTION = new SQLiteConnection(connectionString))
{
DB_CONNECTION.Open();
string sqlquery = "UPDATE table SET Name =#Name, IsComplete=#IsComplete WHERE Key =#Key;";
int rows = 0;
using (SQLiteCommand command = new SQLiteCommand(sqlquery, DB_CONNECTION))
{
SQLiteParameter[] tableA = { new SQLiteParameter("#Key", todo.Key), new SQLiteParameter("#Name", table.Name), new SQLiteParameter("#IsComplete", table.IsComplete) };
command.Parameters.AddRange(tableA);
rows = command.ExecuteNonQuery();
}
DB_CONNECTION.Close();
return (rows);
}
I'm new to c# and i wondering how can I make this work, thanks in advance.
Basically just build up the string sqlQuery based on a set of conditions and ensure that the appropriate parameters have been set. For example, here is some psuedo-C# (not tested for bugs):
//Set to true, so our queries will always include the check for SomeOtherField.
//In reality, use some check in the C# code that you would want to compose your query.
//Here we set some value we want to compare to.
string someValueToCheck = "Some value to compare";
using (SQLiteConnection DB_CONNECTION = new SQLiteConnection(connectionString))
{
DB_CONNECTION.Open();
string sqlquery = "UPDATE MyTable SET Name =#Name, IsComplete=#IsComplete WHERE Key =#Key";
//Replace this with some real condition that you want to use.
if (!string.IsNullOrWhiteSpace(someValueToCheck))
{
sqlquery += " AND SomeOtherField = #OtherFieldValue"
}
int rows = 0;
using (SQLiteCommand command = new SQLiteCommand(sqlquery, DB_CONNECTION))
{
//Use a list here since we can't add to an array - arrays are immutable.
List<SQLiteParameter> tableAList = {
new SQLiteParameter("#Key", todo.Key),
new SQLiteParameter("#Name", table.Name),
new SQLiteParameter("#IsComplete", table.IsComplete) };
if (!string.IsNullOrWhiteSpace(someValueToCheck)) {
//Replace 'someValueToCheck' with a value for the C# that you want to use as a parameter.
tableAList.Add(new SQLiteParameter("#OtherFieldValue", someValueToCheck));
}
//We convert the list back to an array as it is the expected parameter type.
command.Parameters.AddRange(tableAList.ToArray());
rows = command.ExecuteNonQuery();
}
DB_CONNECTION.Close();
return (rows);
}
In this day and age it would probably be worth looking into LINQ to Entities, as this will help you to compose queries dynamically in your code - for example https://stackoverflow.com/a/5541505/201648.
To setup for an existing database - also known as "Database First" - see the following tutorial:
https://msdn.microsoft.com/en-au/data/jj206878.aspx
You can skip step 1 since you already have a database, or do the whole tutorial first as practice.
Here is some psuedo-C# LINQ code to perform roughly the same update as the previous example:
//The context you have setup for the ERP database.
using (var db = new ERPContext())
{
//db is an Entity Framework database context - see
//https://msdn.microsoft.com/en-au/data/jj206878.aspx
var query = db.MyTable
.Where(c => c.Key == todo.Key);
if (!string.IsNullOrWhiteSpace(someValueToCheck))
{
//This where is used in conjunction to the previous WHERE,
//so it's more or less a WHERE condition1 AND condition2 clause.
query = query.Where(c => c.SomeOtherField == someValueToCheck);
}
//Get the single thing we want to update.
var thingToUpdate = query.First();
//Update the values.
thingToUpdate.Name = table.Name;
thingToUpdate.IsComplete = table.IsComplete;
//We can save the context to apply these results.
db.SaveChanges();
}
There is some setup involved with Entity Framework, but in my experience the syntax is easier to follow and your productivity will increase. Hopefully this gets you on the right track.
LINQ to Entites can also map SQL stored procedures if someone one your team objects to using it for performance reasons:
https://msdn.microsoft.com/en-us/data/gg699321.aspx
OR if you absolutely ust compose custom queries in the C# code this is also permitted in Entity Framework:
https://msdn.microsoft.com/en-us/library/bb738521(v=vs.100).aspx
I am using the following code as a controller for a single table to access the table from my database, and assign values of each column to a variable. However, I have many tables(Booktrans in this case) and I want to use a single controller that can access all the tables using table ID as a variable. After that, assigning values of different columns from different tables will also be different from the code below. Could anyone help me with a "dynamic" way of coding to replace this controller for a specific table with a dynamic controller?
I am using MVC4.
All the tables have different structures and different column names.
public ActionResult Booktrans()
{
String connectionString = "Dsn=SECURE;Uid=internwebuser";
OdbcConnection conn = new OdbcConnection(connectionString);
String sql = "SELECT * FROM booktrans";
OdbcCommand cmd = new OdbcCommand(sql, conn);
var model = new List<Booktrans>();
using (conn)
{
conn.Open();
OdbcDataReader rdr = cmd.ExecuteReader();
//model = new List<Booktrans>();
while (rdr.Read())
{
var book = new Booktrans();
book.ship_last_name = rdr["ship_last_name"].ToString();
book.ship_first_name = rdr["ship_first_name"].ToString();
book.ship_zip = rdr["ship_zip"].ToString();
book.ship_state = rdr["ship_state"].ToString();
book.ship_address = rdr["ship_address"].ToString();
book.ship_city = rdr["ship_city"].ToString();
book.day_phone = rdr["day_phone"].ToString();
book.email_address = rdr["email_address"].ToString();
model.Add(book);
}
}
return View(model);
}
How about you use an actual ORM, since that's what you're trying to reverse-engineer here. With something like Entity Framework, you create an entity class that maps to a particular table and that has properties that map to columns in that table. Then, with Entity Framework, in particular, you add a DbSet that represent the collection of rows in this table to your context:
public DbSet<Foo> Foos { get; set; }
With that, you can then simply use the API Entity Framework provides to interact with that data:
// Get specific Foo
var foo = db.Foos.Find(id);
// Get matching Foos
var foos = db.Foos.Where(m => m.Bar == "bar");
// Add a new Foo
db.Foos.Add(new Foo { Bar = "bar" });
So on and so forth. Other ORMS like NHibernate have slight different setups and API, but they all basically behave the same and don't require that you worry about generating SQL.
I am currently converting functions in the DAL of an old app to a new app using Entity Framework/LINQ in its DAL.
There are some instances where I would like to directly pass a string of sql to the database. Is this possible when using LINQ? Here is what I tried atfer research but ExecuteQuery is not available.
using (var context = new DbContext())
{
var sql = #"SELECT DISTINCT * FROM Customer where CustomerId = {0}";
sql = string.Format(sql, customerId);
var query = DbContext.ExecuteQuery<Customer>(sql);
return query.ToList();
}
Doing this seems pretty straight forward but ExecuteQuery is not available to me.
Here is my next attempt at it which seems much better: (please tell me if there is a better way)
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT * FROM CUSTOMERS ");
sql.AppendLine("WHERE #CustomerId = null OR CustomerId = #CustomerId ");
sql.AppendLine("AND #CustomerName = null OR CustomerName = #CustomerName ");
var customerList = context.Customers.SqlQuery(sql.ToString(),
new SqlParameter("#CustomerId", customerId),
new SqlParameter("#CustomerName", customerName)).ToList();
Although for your current condition you can use LINQ.
var customer = context.Customers.Where(c => c.CustomerId = id).Distinct();
This is how you do it Entity Framework Raw SQL Queries
Sending raw commands to the database
using (var context = new DbContext())
{
context.Database.SqlCommand(
"UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1");
}
Writing SQL queries for entities
using (var context = new DbContext())
{
var customers = context.Customers.SqlQuery("SELECT * FROM dbo.Customers").ToList();
}
Loading entities from stored procedures
using (var context = new DbContext())
{
var customers = context.Blogs.SqlQuery("dbo.GE_Customers").ToList();
}
Writing SQL queries for non-entity types
using (var context = new DbContext())
{
var customerNames = context.Database.SqlQuery<string>(
"SELECT Name FROM dbo.Customers").ToList();
}
Update to answer
You do not need to pass SqlParameter, simply pass the default objects
I think the code below should work fine.
var customerList = context.Customers.SqlQuery(sql.ToString(), customerId, customerName).ToList();
if your real query is
sql.AppendLine("SELECT * FROM CUSTOMERS ");
sql.AppendLine("WHERE #CustomerId = null OR CustomerId = #CustomerId ");
sql.AppendLine("AND #CustomerName = null OR CustomerName = #CustomerName ");
I would suggest you to do it this way
var customers = context.Costomers; // this does not populates the result yet
if (!String.IsNullOrEmpty(customerId))
{
customers = customers.Where(c => c.CustomerId = customerId); // this does not populates the result yet
}
if (!String.IsNullOrEmpty(customerName))
{
customers = customers.Where(c => c.CustomerName = customerName); // this does not populates the result yet
}
// finally execute the query
var custList = customers.ToList();
If there is not limitation or requirement don't use this kind of queries they are vulnerable to sql inject attacks.
You can do nearly every sentence with Entity Framework using linq, like the one you wrote
DbContext.Customer.Where(c => c.CustomerId = id).Distinct();
It's more readable, and more secure.
You can use SqlQuery, but it would be better to pass the parameter instead of using Format:
var sql = #"SELECT DISTINCT * FROM Customer where CustomerId = {0}";
var query = DbContext.SqlSuery<Customer>(sql, customerId);
That way, the parameters are SQL encoded so as not to allow for SQL Injection.
I have a requirement where I need to read queries from Access DB in c# and check if the access db query has any keyword like "KEY" if it has keywords I need to enclose that in square brackets"[]".just like how it is done in SQL.
Could someone suggest me how to do that?
You can retrieve the query text like this:
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\...\myDB.mdb";
using (var conn = new OleDbConnection(connString )) {
conn.Open();
string[] restrictions = new string[] { null, null, "myQuery" };
DataTable schema = conn.GetSchema("Views", restrictions);
if (schema.Rows.Count > 0) {
DataRow row = schema.Rows[0];
string queryText = (string)row["VIEW_DEFINITION"];
Console.WriteLine(queryText);
}
}
If you drop the restrictions argument with the query name, conn.GetSchema("Views") returns one row for each query. If you query conn.GetSchema("Procedures") other types of queries like insert, update and DDL statements that are not considered as queries are returned in row["PROCEDURE_DEFINITION"].
View (query) names are returned in row["TABLE_NAME"] and procedure names in row["PROCEDURE_NAME"].
And you can update the query like this:
using (var conn = new OleDbConnection(connString)) {
conn.Open();
var cmd = new OleDbCommand("DROP PROCEDURE myQuery", conn);
cmd.ExecuteNonQuery();
cmd = new OleDbCommand("CREATE PROCEDURE myQuery AS SELECT * FROM myTable", conn);
cmd.ExecuteNonQuery();
}
Strangely enough the OleDb CREATE DDL (Data Definition Language) designates the queries as 'procedures' but the schema table returns a 'VIEW_DEFINITION' and the query name is returned in the column 'TABLE_NAME'. SELECT queries must be retrieved as "Views", other types of queries as "Procedures"; however, both types are created as PROCEDUREs.
While I was testing the answer that #Olivier Jacot-Descombes provided, I was not able to retreive all the queries text representation. Therefore I applied some other method where you open the existing Ms Access database instance and read the queries that are stored in it.
Here is the class I used:
public class MsAccess
{
private Microsoft.Office.Interop.Access._Application _oAccess;
public MsAccess(string path)
{
_oAccess = (Microsoft.Office.Interop.Access._Application)System.Runtime.InteropServices.Marshal.BindToMoniker(path);
}
public string ReturnSqlQueryText(string queryName)
{
string queryDef = null;
var qdefs = _oAccess.CurrentDb().QueryDefs;
foreach (QueryDef qdef in qdefs)
{
if(qdef.Name.Equals(queryName))
queryDef = qdef.SQL;
}
return queryDef;
}
}
Using this code might require you adding using Microsoft.Office.Interop.Access.Dao and Microsoft.Office.Interop.Access both (15.0.0.0) where you can find them under Extension on the reference menu
I'm kinda new to LINQ, so sorry if my question is dumb.
I need to retrieve some values from a database and put them in textBoxes. Just that. Something like the code below, but using LINQ:
EDIT: actually, I want to retrieve more than one field. Like that:
SqlDataReader dr = new functionThatReturnsDataReader();
if (dr.HasRows) {
dr.Read();
txtId = dr["Id"].ToString();
txtName = dr["Name"].ToString();
}
I've found this solution online:
IDbCommand command = dc.GetCommand(query);
command.Connection = dc.Connection;
command.Connection.Open();
IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
However, it seems like I'm trowing away everything that LINQ stands for if I mix it with a standard executeReader. There's no reason in building a data context and query and them execute them like ADO...
How can I achieve the same without using IDbCommand and IDataReader?
I think you need to create a LINQ to SQL class from your database:
http://msdn.microsoft.com/en-us/library/bb384428.aspx
Then you can treat the database table like an object and query it with linq... without the specifics of the query you are trying to run, I cannot construct the linq query for you, but creating the DBML file is the first step.
One thing to think about when comparing a LINQ (or Entity Framework, or other ORM) solution to an ADO.NET solution is that the ORM's are--generally--strongly typed. This means that you need to apply object-oriented principles to them.
If you're dealing with the context, query, and results all in the same function, you can do this:
using(var context = new YourContext())
{
txtId.Text = (from t in context.YourTable
where t.Conditions
select t.Id).FirstOrDefault();
}
If they're not in the same function (and really, they shouldn't be), then something like this would work:
string FunctionThatReturnsId()
{
using(var context = new YourContext())
{
return (from t in context.YourTable
where t.Conditions
select t.Id).FirstOrDefault();
}
}
...
txtId.Text = FunctionThatReturnsId();
If you are using entity Framework then you dont need any query just write id in text box and hit search button it will show you all records in text box of that id.
EntityFramework_mvcEntities db = new EntityFramework_mvcEntities();
int i =Convert.ToInt32( txtsrch.Text);
Employee p = db.Employees.Find(i);
TextBox1.Text = p.Name;
TextBox2.Text = p.Email;
TextBox4.Text = p.Mobile;
db.SaveChanges();
}
var q = from c in context.GetTable<tbl_user>()
where c.user_ID == lbuserid.Text.ToString()
select new
{
c.Username,
c.firstname
};
foreach (var item in q)
{
lbusername.Text = item.Username;
lbfirstname.Text = item.firstname;
}