MySql database backup using c# .Net - c#

I want to create mysql database backup using c#. But this code do some problem because in my database i used "latin1_swedish_ci" thats why after export localize data convert into this form "مرسلی".
try
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (MySqlConnection conn = new MySqlConnection(con))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ExportToFile(path + "\\backup.sql");
conn.Close();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Related

how do I avoid the startup delay when I connect to a MySQL database using c# system.data.mysqlclient visual-studio on mac

Is there a way to force sql connection when the connection is open.. For the first time I wait 20 seconds to display the data.. after that everything is ok..
I use system.data.mysqlclient library for connect to cloud sql.
The code:
void Btn_GetPlaces(System.Object sender, System.EventArgs e)
{
try
{
string cs = #"server=192.168.0.1,3306;userid=Alex;password=Alex123;database=DBMeteo";
var connection = new MySqlConnection(cs);
connection.Open();
LabelSQL.Text = "Connection has beed sucsessfully..";
var cmd = new MySqlCommand();
cmd.Connection = connection;
MySqlCommand command = new MySqlCommand($"CALL nearest3({GlobalLat}, {GlobalLong}, 1)", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// access your record colums by using reader
LabelPlace.Text = (reader[0]).ToString();
LabelDist.Text = (reader[1]).ToString();
LabelCode.Text = (reader[2]).ToString();
}
}
connection.Close();
}
catch (Exception ex)
{
LabelSQL.Text = (ex.ToString());
}
}

Nested methods to insert to database in C# is not inserting all the data

I have a service using 3 methods (each one calling another one) to insert data to 3 tables in sql azure. The first two methods insert the data correctly but the third one (the relationship of the other two) which recieves the indexes of the first two tables doesn't. The methods are something like:
public int InsertTableOne([FromBody]Object obj)
{
int IdTableOne = 0;
tring connectionString = WebConfigurationManager.AppSettings["MyString"];
string queryString = "INSERT INTO TableOne (Name,Phone OUTPUT INSERTED.IdTableOne VALUES (#Name,#Phone) ";
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlTransaction tran = connection.BeginTransaction())
{
using (SqlCommand comm = new SqlCommand(queryString, con, tran))
{
try
{
command.Parameters.AddWithValue("#Name", obj.Name);
command.Parameters.AddWithValue("#Phone",obj.Phone);
IdTableOne = Convert.ToInt32(command.ExecuteScalar());
InsertTableTwo(obj,IdTableOne)
tran.Commit();
}
catch()...
finally
{
con.Close();
}
}
}
}
}
public int InsertTableTwo([FromBody] ob obj,List<listOfElements> listofThings,int IdTableOne)
{
int IdTableTwo = 0;
string queryString = "INSERT INTO TableTwo (Car,Color) OUTPUT INSERTED.IdTableTwo VALUES(#Car,#Color)";
string connectionString = WebConfigurationManager.AppSettings["MyString"];
using (SqlConnection con = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand comm = new SqlCommand(queryString, con))
{
try
{
command.Parameters.AddWithValue("#Car","" );
command.Parameters.AddWithValue("#Color", "");
foreach (listOfElements thing in (listofThings))
{
command.Parameters["#Car"].Value = GetCar(thing.Car);//Methodo to get the car, works fine
command.Parameters["#Color"].Value = thing.Color;
IdTableTwo = Convert.ToInt32(command.ExecuteScalar());
InsertTableThree(IdTableOne, IdTableTwo);
}
}
catch()...
}
}
return IdTableTwo;
}
public int InsertTableThree(int IdTableOne,int IdTableTwo)
{
string connectionString = WebConfigurationManager.AppSettings["MyString"];
string queryString = "INSERT INTO TableThree (IdTableOne,IdTableTwo) VALUES(#IdTableOne,#IdTableTwo)";
using (SqlConnection con = new SqlConnection(connectionString))
{
//connection.Open();
using (SqlCommand comm = new SqlCommand(queryString, con))
{
try
{
command.Parameters.AddWithValue("#IdTableOne", IdTableOne);
command.Parameters.AddWithValue("#IdTableTwo", IdTableTwo);
command.Parameters.Clear();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
return IdTableOne;
}
At the end, the first two tables receive the data but the last one remains empty without the indexes. There's no crash with the database, the transaction or the code. The only problem is that last table data. Also when I debug it, the values of the indexes are correct at the time they're supposed to be inserted.
You are just preparing the command in last method but not executing it. Here is the modified version of third method.
public int InsertTableThree(int IdTableOne,int IdTableTwo)
{
string connectionString = WebConfigurationManager.AppSettings["MyString"];
string queryString = "INSERT INTO TableThree (IdTableOne,IdTableTwo) VALUES(#IdTableOne,#IdTableTwo)";
using (SqlConnection con = new SqlConnection(connectionString))
{
//connection.Open();
using (SqlCommand comm = new SqlCommand(queryString, con))
{
try
{
comm.Parameters.AddWithValue("#IdTableOne", IdTableOne);
comm.Parameters.AddWithValue("#IdTableTwo", IdTableTwo);
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
return IdTableOne;
}

can any one help me how to connect database in oracle and the connection string in c#

protected void Page_Load(object sender, EventArgs e)
{
OracleConnection con = new OracleConnection();
con.ConnectionString = "User id =test;password=test1;Datasource=oracle";
myConnection.Open();
}
Above is the code that I am using. It will be called on page_Load.
Follow below steps,below is my sample example:
1.In Web.config of your file add below string
<connectionStrings>
<add name="CustomerDataConnectionString" connectionString="Data Source=.;User Id=*;Password=*;Integrated Security=SSPI;Initial Catalog=Northwind;OLEDB.NET=True" providerName="Oracle.DataAccess.Client"/>
</connectionStrings>
//* must be filled with your credentials
2.Now in the code behind file,Import namespace for oracle client and Configuration manager for oracle client and below code
using System.Data.OracleClient;
using System.Data;
using System.Configuration;
3.Write below code in your Page_Load event:Cmd can be SQL command
static string strConnectionString = ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString;
using (OracleConnection con = new OracleConnection(strConnectionString))
{
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
using (OracleDataAdapter da = new OracleDataAdapter(cmd))
{
table = new DataTable();
da.Fill(table);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
Refer this link http://www.connectionstrings.com/ for more inforamtion
string oradb = "User id =test;password=test1;Datasource=oracle";
OracleConnection conn = new OracleConnection(oradb);
conn.Open();
using (OracleDataAdapter a = new OracleDataAdapter(
"SELECT id FROM emp1", conn))
{
DataTable t = new DataTable();
a.Fill(t);
// Render data onto the screen
dataGridView1.DataSource = t;
}
conn.Dispose();
Make sure you have included the required libraries,
try using this code ,
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=12345;database=test;";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
It's always better to have try-catch. that helps you track the exact error if you are stuck somewhere.

Import data from one MS Access database into another MS Access database

I am trying to import data from one MS Access database into another MS Access database and have found the following works fine, problem I have got is does anybody know what I should be using if the from database is locked with a SYSTEM.MDW
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Data\Database1.mdb;User Id=admin;Password=;";
string commandText = "INSERT INTO [TableName] SELECT * FROM [MS Access;DATABASE=C:\\Data\Database2.mdb].[TableName]";
try
{
using (OleDbConnection oleConnection = new OleDbConnection(connectionString))
{
using (OleDbCommand oleCommand = new OleDbCommand(commandText, oleConnection))
{
oleCommand.CommandType = CommandType.Text;
oleCommand.Connection.Open();
oleCommand.ExecuteNonQuery();
}
}
}
catch (Exception)
{
throw;
}
I can open the From database using Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\Database2.MDB;System Database=C:\Data\SYSTEM.MDW;User ID=Developer;Password=Password
If you can open the From database, open it and do the action from it:
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\Database2.MDB;System Database=C:\Data\SYSTEM.MDW;User ID=Developer;Password=Password";
string commandText = "INSERT INTO [TableName] In 'C:\\Data\Database1.mdb' SELECT * FROM [TableName]";
try
{
using (OleDbConnection oleConnection = new OleDbConnection(connectionString))
{
using (OleDbCommand oleCommand = new OleDbCommand(commandText, oleConnection))
{
oleCommand.CommandType = CommandType.Text;
oleCommand.Connection.Open();
oleCommand.ExecuteNonQuery();
}
}
}
catch (Exception)
{
throw;
}

How to correctly open and close a SQLite Database in a C# class

Ok so i have a DBConnect class in which I have all my methods relating to the database of my program. I understand that connections are a relatively expensive resource so what is the correct way of opening and closing my database file.
This is what i'm currently using
I simply create the connection in the constructor and then open and close it when needed in the methods. Does this correctly return the connection to the pool? Also would it be better if I put a using(conn) in my methods.
public class DBConnect
{
SQLiteConnection conn = null;
public DBConnect()
{
string connStr = #"Data Source=tests.db; Version=3;";
conn = new SQLiteConnection(connStr);
}
public DBConnect(string connStr)
{
conn = new SQLiteConnection(connStr);
}
public DataTable DBSelect(string query)
{
/*
* Uses a select query to store data from the database into
* a datatable
*/
try
{
conn.Open();
DataTable dt = new DataTable();
using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
{
using (SQLiteDataReader dr = cmd.ExecuteReader())
{
dt.Load(dr);
return dt;
}
}
}
catch (SQLiteException err)
{
MessageBox.Show("Caught exception: " + err.Message);
return null;
}
finally
{
conn.Close();
}
}//DBSelect

Categories