concurrent SqlConnection and EntityConnection to a local Sql Express Server - c#

I'm using Linq2Entity for most of my database operations. However for database creation, table creation and initial data insertion I use plain SQL files. Therefore I need both an SqlConnection and an EntityConnection. Unfortunately the Entity Framework starts complaining that the Sql Server is not listening on the other end of the pipe.
I'm not sure what the problem is here, it could be due to user instancing. Clearing the pool of the SqlConnection or disposing the connection instance does not help.
The connection string I'm using is the following:
"Data Source=.\SQLEXPRESS; Initial Catalog=dbname; Integrated Security=SSPI;"
update:
I have tried to use the EntityConnection for database maintenance purposes but I'm running into troubles. I can't use the EntityConnection to create databases and drop databases. The following syntax is unsupported for an EntityConnection but works fine for an SqlConnection to ms SQL express.
CREATE DATABASE silverfit ON ( NAME = silverfit, FILENAME = 'c:\silverfit\silverfit.mdf' );
Also for some reason the EntityConnection does not allow me to change databases which is necessary to drop a database. I'm afraid I still need the SqlConnection to create the database and tables..
Is there a way for SqlConnections and EntityConnections to coexist for local ms SQL express databases?
Thanks,
Wouter

Have you tried creating a SqlConnection which you then pass to the constructor for your EntityConnection? One of the overloads for EntityConnection takes a MetadataWorkspace and a DbConnection (from which SqlConnection derives.) The slight drawback of this is that you must create your MetadataWorkspace manually, which gathers up the .csdl, .ssdl, and .msl files that define your workspace. However, in the long run, you should be able to share a single connection for both executing DML queries, and using Entity Framework.

I don't agree that you need a plain SQL connection and Entity connection for this task, at least as you've described it. You can execute SQL using the Entity connection. Look at EntityConnection.CreateDbCommand. Naturally, there's a danger here that you are doing DB-server-specific things on a non-DB-server-specific instance like a EntityConnection. But it probably beats having a separate connection, in this case.

Did you try to use the EntityConnection.StoreConnection to retrieve the SqlConnection and execute commands with it ?

If you're trying attaching your database file to SQL Server Express at run time, which it seems as if you are, then only one connection can be open on it at once. This is a problem for other things, not just what you're trying to accomplish. Say, for instance, you connect to 'c:\silverfit\silverfit.mdf' through the server explorer in VS and try to open one of the tables in the db. After you open the table, try running your application. It will bomb.
However, if you open up SQL Management Studio Express (you can download it here), and then attach the database to the SQL Server, the problems you are experiencing should go away. At that point you should be able to open multiple connections to your database, via SQLConnection or EntityConnection.
Attaching your database at run time to the SQL Server express engine really only works well for demoing purposes, or proof of concepts.

Is ADO.NET holding a connection open to the database through connection pooling? Try adding Pooling=False to the connection string, as this may allow the database to be closed before you drop it.

Related

connect database with dataGridView

How can I connect my Trgovina.mdf with dataGridView?
I follow this tutorial, but it seems that program doesn't find my database.
Connection string looks like that:
string connString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Klemen\documents\visual studio 2012\Projects\Trgovina\Trgovina\Trgovina.mdf;Integrated Security=True";
Everything else is the same as tutorial example.
Error string is An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'
Full code looks like this.
The tutorial you talk about in your question use an Access Database and thus uses the OleDB engine to reach and work with the database. Instead your connection string use the syntax reserved for SQLServer LocalDB.
You should change your objects to SqlConnection (instead of OleDbConnection), SqlCommand (instead of OleDbCommand) and so on...
With these changes you should be able to connect to the automatic instance of SqlServer LocalDB. The rest of the tutorial could work or not, depending on what is present in the MDF file used.
You trying to connect to database .mdf file, but you have a wrong provider.
An MDF is a Microsoft SQL Server database not a Jet Database like
Access (*.mdb). You cannot just connect to the flat file and read it.
You would need to mount the database in an instance of SQL Server.
You could install SQL Server 2005 Express
Source
Note: Just download MS SQL Server 2005 Express or later and you must use the System.Data.SqlClient instead of OLE DB to solve your problem.

Create connection to database in VS2010 without SQL Client - dynamically

How to create connection to database in VS2010 without SQL Client dynamically?
I tried using ConnectionStringSettings and SqlConnection and I succeeded, but I'm not supposed to use SQL Client.
I'm using Massive.
You should be able to use System.Data.OleDb.OleDbConnection.
Use a connection string like this:
Provider=SQLOLEDB;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;
It might be that you just miss to reference System.Configuration in your project and consequently Massive cannot find the connection string in your app.config.
Following the instructions on github I could query my SQL Express database without any problems using Massive having a plain SqlClient connection string in my app.config.

Migrating from SQL Server CE to SQL Server database

Currently I am using SQL Server CE for persisting my data for which I am providing with a .sdf and connection string mentioned in app.config pointing to this .sdf file.
Now I want to provide user with the flexibility to have the data stored in their own SQL Server database if present at there disposal.
Now I am facing the problem of how to change the connection string at runtime if user chooses to uses its own database ?
Or if restrict them to use my predefined .mdf file how to attach that in their SQL Server ?
My recommendation would be to have 2 connection strings in the configuration file (app or web). There is a special section for them intuitively called ConnectionStrings. You can then switch between them based on other settings.
Changing connection strings dynamically is actually pretty easy to do as long as you have a place to store the new settings (ie. web or app config files). If you provide a way for them to enter the server information you can use the ConfigurationManager class to update your app/web.config.
Ado.net typically has parameters on almost any db connection object that allows you to specify the connection string as an arguement. Additionally, there are helper classes that can be used to construct the connection string on the fly like the SqlConnectionStringBuilder or EntityConnectionStringBuilder. I personally love the Entity Framework as it allows you to create the database from the model itself if it does not already exist, provided you already have the connection string.

Join tables from two databases in C#

With SQL Server I run this query with no problem...
SELECT SUM(Esi) AS Dispo
FROM [mdb].[dbo].[Query1] AS A
INNER JOIN [mdb2].[dbo].[TieCol] as B ON A.Alias=B.IDAlias
WHERE Alias LIKE 'SETUP%'
I join two tables that reside in two different databases (mdb and mdb2).
But how can I do it in my .NET application?
When I need to use this statement
string cmdText = "SELECT SUM(Esi) AS Dispo
FROM [mdb].[dbo].[Query1] AS A
INNER JOIN [mdb2].[dbo].[TieCol] as B ON A.Alias=B.IDAlias
WHERE Alias LIKE 'SETUP%'";
this.OP = new SqlConnection(ConfigurationManager.ConnectionStrings["mdb2"].ConnectionString);
SqlCommand sqlCommand = new SqlCommand(cmdText, this.OP);
I can't execute it, since this.OP is the connection to mdb2... And for mdb?
How can I connect to both databases simultanously?
The SQL connection is to the server - the Initial catalog in a connection string behaves like use - it sets the default DB.
So your 3 part SQL query should work as is. So possibly
Make sure that the SQL login used by your app (or the account of your AppPool if using Web and Integrated Security) has the necessary access to both databases. (use RunAs on SQL Enterprise Manager as this account and try to run the query)
You might try escaping [Alias]
Also, if there is coupling between mdb1 and mdb2 (e.g. SPROCS in mdb1 use tables in mdb2 etc), for ease of maintenance, you might consider adding views in mdb1 for mdb2 objects. This allows for easy identification of cross-database dependencies. In this case, your query can use views which look like they are in the same database, although the underlying dependency on mdb2 is still there.
I'm not sure if there is a way to do this within the connection string. But you can probably do it using a four part reference to the table: [server].[database].[table].[column].
Your C# application only need to connect to one database server for this query.
Say your C# application connect to [mdb]. Database [mdb2] should b linked server in database [mdb].
Since you can run that query in sql server, so there must be one sql server connected to both databases. use that sql server in your C# connection string. That's it!

Keyword not supported 'provider' error

c#.Net 3.5 with a SQL Server 2000 backend, I have a connection string in my app.config file that looks like this
<add name="MFG_ConnectionString"
connectionString="Provider=SQLOLEDB;Data Source=MFG;Persist Security Info=True;Password=kb1234;User ID=kb;Initial Catalog=MFG"
providerName="System.Data.OleDb" />
This connection string was built with the data source configuration wizard. Creating a dataset with this and dragging the DataSource element to create a DataGridView populates and successfully allows all CRUD operations.
However, I'm not looking to make changes to this through a databound form. I'm looking to do this behind the scenes in code. Since this is an older version of SQL Server I'm assuming I must use OleDbConnection and other OleDb objects to get the job done. When I try to execute the following:
OleDbConnection visualConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["MFG_ConnectionString"].ConnectionString);
I get an exception: "Keyword not supported 'provider'.
Yet if I take out provider I'm told that I must supply one. Not sure why this works through the dataset on the form yet I cannot create my own connection object... any thoughts?
EDIT It should be noted that when I originally created the connection to this database, it told me that the database I was trying to connect to did NOT support SqlConnection and that I must choose another (my choice being OleDb at that time). It is odd to me that this connection works behind the scenes as SqlConnection without provider in the connection string but the dataset then breaks...
SqlConnection works fine with SQL 2000. You can get a connectionstring sample here

Categories

Resources