Frage

I am creating a client application that connects to a website in order to execute queries. I am not enabling the client to connect directly to the database but he can perform queries through the website.

The way I execute queries through the website is through linq. For example I may do:

MyEntities db = new MyEntities();
var customers = db.Customers.ToList();

Since the client does not have the connection string and sql server does not allow remote connections when he executes the above code it will obviously not work. My question is how can the client send that query to the web service?

The reason why I need this is because there are so many different types of queries and for each different one I have to create a different page. For example I have GetInvoices.aspx, GetCustomers.aspx, and I am always creating new ones just because I dont want the client to connect directly to the database. It will be nice if I could serialize the linq query and send that to the server. the server then should validate that I am not doing a delete statement for example and if thats the case then execute the query.


Eidt

This is what I am going to do for only select statements:

// Note connection string only have basics. It does not have password nor database. 
public static string GenerateSelectQuery<T>(Func<Common.Data.TcEntities, IQueryable> method)
{
    Common.Data.TcEntities db = new Common.Data.TcEntities(@"metadata=res://*/Data.Model1.csdl|res://*/Data.Model1.ssdl|res://*/Data.Model1.msl;provider=System.Data.SqlClient;provider connection string=""""");
    var query = method(db);
    return query.ToString();            
}

then if I wish to create a custom query I will do:

var query = GenerateSelectQuery<Customer>(db => db.Customers.Where(x=>x.FirstName.Contains("a")));

I will send then that string to the server and expect an array of Customers. On the server side I will make sure string starts with select and it does not contain --.

War es hilfreich?

Lösung

Implementing a WCF Data Services, http client can query your data using the OData protocol.

For example, applying a select Name on your customers collections will be queryable using the http url:

http://youdomain/yourWCFDataServices.svc/Customers()?$select=Name
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top