Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6723697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:35:59+00:00 2026-05-26T09:35:59+00:00

I am trying to restore the database file(backup.sql) using c# by the following method…

  • 0

I am trying to restore the database file(backup.sql) using c# by the following method…

                string hostaddress = "localhost";
                try
                {
                  string pathforbackupfile = @"C:\folder\Access\backupdb\backup.sql";
                  StreamReader file = new StreamReader(pathforbackupfile);
                  string input = file.ReadToEnd();
                  //file.Close();

                  ProcessStartInfo psi = new ProcessStartInfo();
                  psi.FileName = @"C:\wamp\bin\mysql\mysql5.5.8\bin\mysql.exe";
                  psi.RedirectStandardInput = true;
                  psi.RedirectStandardOutput = true;
                  psi.CreateNoWindow = true;

                  psi.Arguments = string.Format(@"-u {0} -h {1} {2}", "root", hostaddress, "access");
                  psi.UseShellExecute = false;
                  Process p = Process.Start(psi);
                  //p.StandardInput.WriteLine(input);
                  p.StandardInput.Write(input);
                  p.StandardInput.Close();
                  p.WaitForExit();
                  p.Close();

but i have failed to restore the database..

what is wrong with the above code..

EDIT :

this is my one of the table structure :

--
-- Table structure for table `banks`
--

DROP TABLE IF EXISTS `banks`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `banks` (
  `bank_Id` int(11) NOT NULL AUTO_INCREMENT,
  `bank_Tag` varchar(32) NOT NULL DEFAULT '',
  `bank_Name` varchar(32) NOT NULL DEFAULT '',
  `bank_Address1` varchar(32) NOT NULL,
  `bank_Address2` varchar(32) NOT NULL,
  `bank_Address3` varchar(32) NOT NULL,
  `bank_Town` varchar(32) NOT NULL,
  `bank_County` varchar(32) NOT NULL,
  `bank_Postcode` varchar(32) NOT NULL,
  `bank_Country` varchar(32) NOT NULL DEFAULT 'UK',
  `bank_Contact` varchar(32) NOT NULL DEFAULT '',
  `bank_Phone` varchar(32) NOT NULL DEFAULT '',
  `bank_Email` varchar(32) NOT NULL DEFAULT '',
  `bank_SortCode` varchar(16) NOT NULL DEFAULT '00-00-00',
  `bank_Active` varchar(8) NOT NULL DEFAULT 'yes',
  PRIMARY KEY (`bank_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `banks`
--

LOCK TABLES `banks` WRITE;
/*!40000 ALTER TABLE `banks` DISABLE KEYS */;
INSERT INTO `banks` VALUES (1,'ne','asd','asdf','df','df','df','df','sdf','df','df','df','sdf','sdf','y');
/*!40000 ALTER TABLE `banks` ENABLE KEYS */;
UNLOCK TABLES;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:36:00+00:00Added an answer on May 26, 2026 at 9:36 am

    This is part of a function I wrote long time ago.
    Hope this helps:

        private MySqlConnection NewConnection(string host, int port, string database, string user, string password)
        {
            if (port <= 0) port = 3306;
            string cstr = "";
            if (database == "")
                cstr = String.Format("SERVER={0};PORT={1};UID={2};PWD={3}",
                    host, port, user, password);
            else
                cstr = String.Format("SERVER={0};PORT={1};DATABASE={2};UID={3};PWD={4}",
                    host, port, database, user, password);
            MySqlConnection conn = new MySqlConnection(cstr);
            try { conn.Open(); }
            catch (Exception ex)
            {
                conn = null;
            }
            return conn;
        }
    
        public int ExecuteScript(MySqlConnection conn, string sql)
        {
            err = "";
            int totrows = -1;
            if (conn == null) return totrows;
            if (conn.State == System.Data.ConnectionState.Closed) conn.Open();
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            try
            {
                totrows = cmd.ExecuteNonQuery();
                cmd = null;
                return totrows;
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return 0;
            }
        }
    
    public bool Restore(string dbHost, int dbPort, string dbUser, string dbPassword, string file)
    {
        int toterr = 0;
        int totsql = 0;
        string msql = "";
        StreamReader rd = null;
        FileStream fs = null;       
    
        MySqlConnection conn = NewConnection(dbHost, dbPort, "", dbUser, dbPassword);
        if (conn == null) return false;
        int cur = 0;
        int rows = 0;
    
        fs = new FileStream(file, FileMode.Open);
        rd = new StreamReader(fs);
        while (rd.Peek() > 0)
        {
            string t = rd.ReadLine().Trim();
            if ((t == "") || (t.StartsWith("--"))) continue;
            msql += t;
            if (msql == ";") msql = "";
            if (msql.EndsWith(";"))
            {
                string s = msql.Remove(msql.Length - 1, 1).Trim();
                msql = "";
                try
                {
                    rows = ExecuteScript(conn, s);
                }
                catch
                {
                    toterr++;
                    break;
                }
                finally
                {
                    cur++;
                }
            }
        }
        rd.Close();
        rd = null;
        fs.Close();
        fs = null;
    
        conn.Close();
        conn = null;
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to restore a database by using the following code string Restore =
I am trying to restore a backup file to SQL Server 2008. I am
I am trying to restore a database (from file thedb.bak ). I am using
I am trying to restore MySQL database data from a dump file, using C#
I'm trying to restore a database (SQL SERVER 2008) from a backup in a
I'm trying to restore a database from an sql file through phpmyadmin import function.
I am trying to restore a database by first restoring a full backup and
I'm trying to restore a PostgreSQL database by executing the SQL that pg_dump created,
I am trying to restore a DB using an SQL script, but things foreign
I'm trying to perform some offline maintenance (dev database restore from live backup) on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.