Create connection to database in VS2010 without SQL Client - dynamically - c#

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.

Related

How to add connection string to once for whole solution to use?

Recently I am working on an .Net project. We used EF to handle SQL, when we make an installer of the program, we realize that app.config is visible which mean that the connection string is not safe.
I am looking for a way to add connection string (or maybe secret code and username) to the EF so that the connection string is not visible.
Something like change old code from this
Using db As ConnectDb.adoSentoEntities= New ConnectDb.adoSentoEntities
'TODO
End Using
to this
Using db As ConnectDb.adoSentoEntities= New ConnectDb.adoSentoEntities(ConnectionString)
'TODO
End Using
But since we used connect code to SQL all over the place, changing every single line of code is not possible. There is a way I only need to add connection string once?
You’d be better off encrypting the connection string section in the app.config. You wouldn’t need to make any changes.
Storing any sort of configuration in an assembly can be read using a hex editor.
It’s been answered on here before.
Encrypting Connection String in web.config
You’d be better off using a trusted connection if you’re using SQL Server. The user running the app would need to have permissions and no username and password is required.
Save connection string is settings of project properties.
Go in project properties.
Select settings.
Add new setting as connection string and save connection string.
Then you can use it for whole project.

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.

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.

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

concurrent SqlConnection and EntityConnection to a local Sql Express Server

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.

Categories

Resources