Change datasource in Visual Studio 2012 from SQLSRV to MySQL - c#

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

Related

Unable to connect to SQL Server database using 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.

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.

Save SQL DataSet to Local MDB File

Context
My appliction uses an SQL database from which it reads my datatables at start of my application. If the application would fail to connect to the SQL DB, I have a local Ms Access .MDB file. I have a separate thread that checks if the local database is outdated.
I have a DataTable which I obtain from my SQL connection --> Verified and working
I can connect to my Access database locally and read from it --> Verified and working
Issue/Question
I'm trying to update my local database by updating it with the DataTable I obtained from my SQL Connection.
public static void UpdateLocalDatabase(string strTableName, OleDbConnection MyConnection, DataTable MyTable)
{
try
{
if (CreateDatabaseConnection() != null)
{
string strQuery = "SELECT * FROM " + strTableName;
OleDbDataAdapter MyAdapter = new OleDbDataAdapter();
OleDbCommandBuilder MyCommandBuilder = new OleDbCommandBuilder(MyAdapter);
MyAdapter.SelectCommand = new OleDbCommand(strQuery, odcConnection);
MyAdapter.UpdateCommand = MyCommandBuilder.GetUpdateCommand();
MyConn.Open();
MyAdapter.Update(MyTable);
MyConn.Close();
}
}
catch { }
}
If I debug this snippet, all variables are what they should be:
strTableName = the correct name for my table
MyConn = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyLocation;Persist Security Info=True;JET OLEDB:Database Password=MyPassword;"
MyTable = is the correct table that is also used further on by my application
This process runs through without an error and without using the catch but it does not touch my database, it just doesn't do a thing.
Am I dropping the ball here or just missing the obvious, I have no idea but I browsed many articles and apart for showing the MyAdapter.Update(), there doesn't seem to be much more to it.
Any help is welcome.
Thanks,
Kevin
Does your backup database have to be in access? because if you used SQL Compact Edition it'd be much easier to copy between the two?
Yes, it would either mean attaching it with your installer or just ensuring that all client machines have it pre-installed, it is free however.
if this is an issue then all you need to do (I think, not done it myself)
would be to go to your installer projects properties, click prerequisites and then tick SQL compact so that it will be installed before your application can be used, iv done this before with other frameworks and it just pops up a box with the install shield asking whether they want to download the necessary software and its just one click then it should be done for them.
Do you need a hand on using the compact database also?
One negative by the way is it does lack some higher end features but shouldn't affect average database work
EDIT
if you will be using sql CE you can easily make the databse in VS by clicking data and new data source then following the steps making sure to put sql CE when asked
if it works, you'll end up with an .sdf database
I provided a code snippet that fixed the issue on my related question here: Export SQL DataBase to WinForm DataSet and then to MDB Database using DataSet

Convert Access 2010 to SQL 2008

just writing a tool (c# .Net 4) to convert an existing Access 2010 database to SQL Server 2008.
At the moment i have to convert the data types and build the "create" commands for table and the constraints manually.
So whats the fastest way to get the schema from Access with the DataAdapter and create a table from the DataTable at the SQL Server with all the constratins and converted typs ?
OleDbDataAdapter da = new OleDbDataAdapter(accCmd);
DataTable accSchema = new DataTable();
da.FillSchema(accSchema, SchemaType.Source);
//now create db table at sql server from accSchema
Thanks for your help.
Gpx
I have used this in the past when upsizing my access applications and it did a good job
Microsoft SQL Server Migration Assistant for Access v5.2
Are you sure you can't use an existing tool? Saves re-inventing the wheel.
http://projects.c3o.com/files/3/plugins/entry11.aspx
http://dbcopytool.codeplex.com/

How to Connect Crystal Reports to MySQL directly by C# code without DSN or a DataSet

How can I connect a Crystal Report (VS 2008 basic) to a MySQL DB without using a DSN or a preload DataSet using C#?
I need install the program on several places, so I must change the connection parameters. I don't want to create a DSN on every place, nor do I want to preload a DataSet and pass it to the report engine. I use nhibernate to access the database, so to create and fill the additional DS would take twice the work and additional maintenance later. I think the best option would be to let the crystal reports engine to connect to MySQL server by itself using ODBC.
I managed to create the connection in the report designer (VS2008) using the Database Expert, creating an ODBC(RDO) connection and entering this connection string
"DRIVER={MySQL ODBC 5.1 Driver};SERVER=myserver.mydomain"
and in the "Next" page filling the "User ID", "Password" and "Database" parameters. I didn't fill the "Server" parameter. It worked. As a matter of fact, if you use the former connection string, it doesn't matter what you put on the "Server" parameter, it seems the parameter is unused. On the other hand, if you use "DRIVER={MySQL ODBC 5.1 Driver}" as a connection string and later fill the "Server" parameter with the FQDN of the server, the connection doesn't work.
How can I do that by code? All the examples I've seen till now, use a DSN or the DataSet method. I saw the same question posted but for PostgreSQL and tried to adapt it to mysql, but so far, no success. The first method:
Rp.Load();
Rp.DataSourceConnections[0].SetConnection("DRIVER={MySQL ODBC 5.1 Driver};SERVER=myserver.mydomain", "database", "user", "pass");
Rp.ExportToDisk(ExportFormatType.PortableDocFormat, "report.pdf");
raise an CrystalDecisions.CrystalReports.Engine.LogOnException during ExportToDisk
Message="Logon failed.\nDetails: IM002:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.\rError in File temporal file path.rpt:\nUnable to connect: incorrect log on parameters.
the InnerException is an System.Runtime.InteropServices.COMException with the same message and no InnerException
The "no default driver specified" makes me wonder if the server parameter is unused here too (see above). In that case: How can I specify the connection string?
I haven't tried the second method because it doesn't apply.
Does anybody know the solution?
I think it'll likely be quicker to generate the Dataset via nHibernate, or do a direct ADO.NET query, then trying to solve the issue.

Categories