ADO/ADO.NET - how to check the ms access database is there or not.please help me

Asked By Bhanuprakash T.R on 21-Jun-12 12:41 AM
Hello sir i placed one  button(Writedb) click means . what are the data in sdf file . whole data inserted into access sheet. it is also working fine. the file was stored in the path C:/StockManagement/StockManagement.mdb. if i compile first time means it was created successfully. if again compile means it shows an runtime error is database already exist. now i want solution was if the database is already is there means delete the database and created new one.if the database is not there created new one..please tell me the solution
i try this code
//button click
Directory.CreateDirectory("C:/StockManagement");
       ADOX.CatalogClass cat = new ADOX.CatalogClass();
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data Source=C:/StockManagement/StockManagement.mdb;" +
        "Jet OLEDB:Engine Type=5");
        MessageBox.Show("database created successfully");
        cat = null;
        if(cat.ex)
        MessageBox.Show(pra);
        SqlCeConnection con = new SqlCeConnection("Data Source=" + textbox1.text.toString + "");
        SqlCeDataAdapter da = new SqlCeDataAdapter("select * from purchaseOrder", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        OleDbConnection con1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/StockManagement/StockManagement.mdb");
        con1.Open();
        OleDbCommand cmd = con1.CreateCommand();
        cmd.CommandText = "create table purchaseOrder(barcode varchar(150),Actualqty varchar(150),LocationID varchar(100))";
        cmd.ExecuteNonQuery();


        for (int i = 0; i < dt.Rows.Count; i++)
        {
          cmd.CommandText = "insert into purchaseOrder (barcode,Actualqty,LocationID) values('" + dt.Rows[i]["barcode"].ToString() + "','" + dt.Rows[i]["Actualqty"].ToString() + "','" + dt.Rows[i]["LocationID"].ToString() + "')";


          cmd.ExecuteNonQuery();

        }

please help me
Jitendra Faye replied to Bhanuprakash T.R on 21-Jun-12 12:46 AM
Before connecting to DataBase you can check existence like this-

if(File.Exists(@"C:/StockManagement/StockManagement.mdb") )
{
   //Your code
}
else
{
  //Not exists
}


Try this and let me know.
Bhanuprakash T.R replied to Jitendra Faye on 21-Jun-12 01:13 AM
if (File.Exists(@"C:/StockManagement/StockManagement.mdb"))
        {
}
else{

          MessageBox.Show("file was trying to create");
          Directory.CreateDirectory("C:/StockManagement");
          ADOX.CatalogClass cat = new ADOX.CatalogClass();
          cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
          "Data Source=C:/StockManagement/StockManagement.mdb;" +
          "Jet OLEDB:Engine Type=5");
          MessageBox.Show("database created successfully");
          cat = null;
          MessageBox.Show(pra);
          SqlCeConnection con = new SqlCeConnection("Data Source=" + pra + "");
          SqlCeDataAdapter da = new SqlCeDataAdapter("select * from purchaseOrder", con);
          DataTable dt = new DataTable();
          da.Fill(dt);
          OleDbConnection con1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/StockManagement/StockManagement.mdb");
          con1.Open();
          OleDbCommand cmd = con1.CreateCommand();
          cmd.CommandText = "create table purchaseOrder(barcode varchar(150),Actualqty varchar(150),LocationID varchar(100))";
          cmd.ExecuteNonQuery();


          for (int i = 0; i < dt.Rows.Count; i++)
          {
            cmd.CommandText = "insert into purchaseOrder (barcode,Actualqty,LocationID) values('" + dt.Rows[i]["barcode"].ToString() + "','" + dt.Rows[i]["Actualqty"].ToString() + "','" + dt.Rows[i]["LocationID"].ToString() + "')";


            cmd.ExecuteNonQuery();

          }

}

i try above code but i got run time error in this line cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
          "Data Source=C:/StockManagement/StockManagement.mdb;" +
          "Jet OLEDB:Engine Type=5");  interface error