I was reading that linq was lazy and that it did not executed the query until it needed to.
if that is the case why does this code fails:
var db = new Data.DataClasses1DataContext(#"Data Source=.\sqlexpress;Initial Catalog=MyDb;Integrated Security=True");
var companies = db.Customers.Where(x => x.Company=="Foo");
var query = companies.ToString();
if I run that code in a computer that does not have sql server installed it will not run why? I am not doing any statement that needs data. If I would call companies.ToList() then its ok for the code to fail. Is there a way I can make use of Linq to SQL Classes without using a connection. I know the moment I do ToList() or try to enumerate through the results I will get an error. I just want to use Linq to Sql Classes in order to generate the SQL statements and see them as a string.
I have a client and a server. The server is a WCF service and the client is a console application. I will send the query encrypted for cases where the user is not entering it. I will like to generate my queries using Linq to Sql classes it does not make sence I have to install sql server on the client just so that I can generate the queries.
My temporary solution is to create a second database on the same server. That database will be allowed to accept remote connections and the whole purpose of it is so that the line
var db = new Data.DataClasses1DataContext(#"some remote connection string");
works. Once I initialize that line I will never need the connection again. It makes no sense.
Do not generate queries on the client then pass the SQL to the service. Instead, generate the lambda expression on the client, and send the expressions to the service.
See "How can I pass a lambda expression to a WCF service?".
One problem this will solve is that of database and schema versioning. TO do it your way would require that the client understand the database schema and even database version, and that it be the same (or compatible) with that which the service uses. Otherwise, you would be stuck having the SQL for one version of SQL Server generated on the client, then sent to a different SQL Server version on the service (or equivalently, a different database schema).
The problem is in the creation of the db context object and not in the linq statement. Specifically, in order to create the db context object you need an actual connection string. If you don't provide one, then the db context you try to create, I suppose it would be null or you will get an exception. Then defining your linq query using this null object will throw an exception, even if your query doesn't use the ToList(), which will definetely force the execution of your linq query.
Reading again your post I believe that you should define in the connection string the sql express server that is installed in the server, which will host the WCF service. Then the client having this connection string would have the ability to make calls to your server database.
Related
Based on the link here which talks about connection pooling I see we are creating a new SqlConnection object which takes a parameter 'connectionString'
How to use connection pool without passing the connection string? We retrieve the connection string securely but across the application we are passing around the string which makes the connection string available in memory dumps.
I am looking for a similar approach in C# way it is done in Java. We create the datasource object and ask for a connection but we do not pass around the connection string.
How to achieve the same in C# ADO.NET connection pools?
TIA
Edit: What I meant by passing around the string (this code is present in every method in database access layer):
using (SqlConnection connection = new SqlConnection($conn_string))
{
connection.Open();
// execute queries
}
If you are concerned about database credentials in memory dumps, don't use them.
Instead, you can
Use SQL Server integrated authentication
If for some reason you can't work with a domain service account, use a DSN
If you still want a factory you can use something like the EF's SqlConnectionFactory or of course write your own.
I am trying to use entity framework 6.1.3 to connect with remote MySql server (MySql.Data.Entity 6.9.9). Connection doesn't fails, but my code do unexpected things:
screenshot
It is interesting, that this code run in expected way on my local MySql DB with exactly same schema and exactly same records.
Does anyone have ideas, why code doen't work on remote DB? How to fix this bug?
EDIT:
Code also works fine, for example, with comparing integer primary key. Problem with string comparation? It's easy exetutes ToList() for all records.
Problem was with charset. Just add "charset=utf8" (my remote db default charset) to connection string.
I need to extract rows from a SQL table where some columns are encrypted using SQL Server's new 'Always Encrypted' feature. I see that I cannot use the 'AZURESQLDB' DataSource feature and there needs to be decryption done before reading the data in plain text. Are there plans to add this capability?. Meanwhile, I tried to write a user defined function that will do the same operation(connect, decrypt data and return object) in a registered assembly but when it runs, I get the following error:
Inner exception from user expression: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I have checked the code and everything seems correct. The connection string is used by the SqlConnection object and works fine in all other applications. I am guessing that the connectivity to external data sources from within a UDF is blocked. Is there any way around this?
Are you using the DATA SOURCE in U-SQL for representing your SQL Server instance and you cannot get it to read encrypted data? If so, please file a feature request at http://aka.ms/adlfeedback.
You cannot call out to network resources directly from within U-SQL user code for the reasons explained here.
One way around this might be to create a stored procedure which does the hard work, the decryption then renders the data. Then use Azure Data Factory with a Stored Proc Task to access the decrypted data and move what you need to the Data Lake - not including the secure data. From there you could then access it using a U-SQL script. One idea? Let me know if you need me to work up more of an example.
I am using Servicestack OrmLite as a data layer for my application (.NET C# 3.5/SQL Server).
One of the design requirements (It isn't greenfield, so it is mandatory) is to have commands executed as a particular user which has a schema attached on the SQL server side.
After creating the DbContext with OpenDbConnection() I send an Execute as User command to SQL server so that they are executing with the correct login, and they are switched over to the correct schema for that login.
The error I am getting back for selects against that connection later in the process is:
A severe error occurred on the current command. The results, if any, should be discarded.
Thoughts:
Connection pooling is losing the current user command sent to SQL server?
Is there a built in User/schema handler extension to OrmLite that I haven't seen?
RegisterConnection?
Thanks for your input.
Does LINQ to SQL work in connected environment or disconnected environment? I mean if you compile the query it builds expression query and the query is sent down to sql server ,there it is translated into T-SQL statement and executed and the final result is sent back to the C# code.I hope it is working in connected environment.Is there any framework operates LINQ to SQL in disconnected fashion?
It is connected when it needs to be - i.e. when querying data and when submit changes. If you supply a connection (to the overloaded constructor) that connection will be re-used, otherwise IIRC connection-pooling is used.
For fully disconnected, there are things like "sync services", which builds a local copy of the data.