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;
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;
}