Unable to connect to SQL Server database using C# - c#

I'm currently working on a wpf application where I tried to create a database.
I used data sources > add new datasource > dataset and copied the query string for its properties, but it is giving me the following exception:
What might be the problem? This is a local database... and when I click on the test connection button it writes "test connection succeeded"
Thanks

You are connecting to an SDF file. This means that you are using Sql Server Compact, not the full fledged Sql Server.
The classes to be used are named
SqlCeConnection
SqlCeCommand
The one you are using (SqlConnection) cannot understand the connection string used for a Sql Server Compact
Of course you need to add the reference to the assembly and the appropriate using directives
Assembly: System.Data.SqlServerCe.dll
using System.Data.SqlServerCe;
....

You are using a SqlConnection rather than the SqlCeConnection that you require. SqlConnection is for connecting directly to a "real" sql server.
Take a look at the MSDN for more information.

Related

Change datasource in Visual Studio 2012 from SQLSRV to MySQL

I have a basic Application in Visual Studio 2012 which is connected to Sql Server 2008 R2 but now I want it to be connected only to a MySQL database (whose tables and columns are the same).
I downloaded MySQL for Visual Studio and I got a successfully connection in Data Sources to my database in MySQL.
Now I want to ask, what should I change from my app (apart of the connection string) if it only does simple CRUD with the db?
For example I've changed connection strings and when the app tried to search in db and fill a dataset I found an error in this code:
SqlDataAdapter dataAdapter = database.getPaquetes(date, _destino);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dgv_paqdisp.ReadOnly = true;
dgv_paqdisp.DataSource = ds.Tables[0];
ERROR:
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).
Are those types only for SQL Server? If yes, what is an equivalent when using MySQL?
Notice I don't want you to re-make my code or anything, I only want to know if there is something I'm missing or if its not that simple to change DataSource? Thanks in advance.
As WearWolf write you need a Mysql Connector/Net. You can download it from Nuget as packages. For the code that you have written for Sql Server will need to modified too much to get it worked on MySQL.
for example
SqlConnection will be MySqlConnection
SqlCommand will be MySqlCommand
You can do this changes Be Manual Find replace for every .cs file in solution carefully. At then end you will got working your app in MySQL.
You didn't mention that your CRUD is written using Entity framework and data-source tag make confusion here.
If you use Entity framework then simply generate the entity from database, because Mysql is a different database system you need to make the table in your mysql database first.
or if you use SQl queries execution for CRUD then just use given Find and replace and it will works.
You'll neet to add a reference to MySQL Connector/NET assemblies. They should come with MySQL for Visual Studio, otherwise that link has download instructions.
Then your code would become something like
MySqlDataAdapter dataAdapter = database.getPaquetes(date, _destino);
It shouldn't change much since both implemented using ADO.net

How do I used a database I created in MS SQL Server 2008 R2 with a VS C# Project?

I created a new database using SQL Server 2008 R2 by using the Management Studio. The connection says (local) and I am using Windows Authentication (though I installed with mixed mode).
My questions are:
How do I connect to the DB via my C# application -
The only time I ever have done this before I just used VS Menu > Tools > Connect to DB and the drop down saw my database and connected, then right clicked on it and grabbed the connection string for use in connecting. However I'm thinking because its (local) I don't have that option.
As per Q#1 I am assuming the database file is being stored somewhere locally - I am wondering how to find that location and how I can include it with my application
Edit** Per comment: VSMenu-> View-> Server Explorer and then use add connection to connect to your local SQL Server instance and then use the database you created from the databases dropdown, and from advance settings copy the connection string created by the connection dialog
This is what I am looking for but I am missing the step during "add connection" where do I find my SQL Server I created locally? As mentioned before I have no idea where it is stored or how to find it
MSDN has an example in the SQLConnection documentation
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(queryString, connection))
{
command.Connection.Open();
command.ExecuteNonQuery();
}
You can then optionally use a SqlDataAdapter.
You need to use ADO.NET, which is comprised of a connection object (SqlConnection), command object (SqlCommand), parameters objects (SqlParameter) and data sets (DataSet) or data readers (SqlDataReader).
Read A Beginner's Tutorial for Understanding ADO.NET.

Can't connect to my SQL Server database?

I'm creating a very basic CRUD desktop winforms application in C#/.NET 4.0.
Letting Visual Studio auto-generate the fields for the table I'd like to perform my CRUD operations on works just fine, but I'm running into problems when I try and interact with it manually with my own SQL queries.
The auto-generated fields are using the connection string:
Data Source=|DataDirectory|\Data Analysis.sdf
If I try and do:
SqlConnection conn = new SqlConnection(#"Data Source=|DataDirectory|\Data Analysis.sdf");
conn.Open();
It just hangs. What am I missing?
That's a connection string for a SQL Server Compact Edition (CE) database (everything stored inside a single .sdf file) - is that what you're using?
If so : in that case, you'd have to use SqlCeConnection (not a SqlConnection - that's for "grown-up" SQL Server version - not CE)
Maybe try adding some more options to the connection string:
Persist Security Info=False;
File Mode=shared read;
Believe you've specified a relative path to the .sdf file, where you might need to get the executable's runtime folder from System.Environment.CurrentDirectory and prepend it to the filename.

Find connection string for connecting to MySQL Server 5.1.50 using OleDbConnection

I am interested how can I find the connection string in order to be able to connect to MySQL Server 5.1.50 using OleDbConnection(C#).
I used this auto generated string (after adding new data source in Visual Studio):
server=localhost;User Id=MyID;password=MyPassword;database=MyDatabase
but I always get the same error message:
An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'.
I have tried different providers but neither of them seems to work.
Is there a reason you aren't using the MySQL .NET connector? Anyways, I think you need to add
Provider=MySQL Provider; to your connection string.
try this as your connection string:
Provider=MySQL Provider;server=localhost;User Id=MyID;password=MyPassword;database=MyDatabase;
The MySQL.NET connector fully implements the ADO.NET interface. Every command is identical to using the System.Data.SqlClient namespace.

C# CLR Assembly Query

I am creating a C# assembly for MS SQL Server 2005 for encryption/decryption.
I need to query the database in this assembly, and was wondering what the preferred method of connecting to the database is? I believe we would not want to have a username/password in the connection string.
Since the assembly is registered in MS SQL does it have some sort of quick way to access data in the database?
I'm a little bit of a newb as it related to Integrated Security or Trusted Connections.
You can use the following connection string when using CLR stored procedures:
using(SqlConnection connection = new SqlConnection("context connection=true"))
{
// ..
}

Categories