C# .NET - It shows error Ms Access database already exist in c#.net

Asked By Bhanuprakash T.R on 20-Jun-12 02:06 AM
Hello Sir,

     i am browse sdf file in textbox. then i click on button(Write DB) means what are the data having sdf database convert into Ms access .. i try this code but it says database already exist. now i want if the mdb file is not there means i want create and insert data or else update new data into same mdb file.

        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=" + textbox1.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();

        }

how to check if stockmanagement.mdb file is already there means update or else create and inserted... please tell me how to implement the if and else condition

Thanks
Bhanuprakash
Jitendra Faye replied to Bhanuprakash T.R on 20-Jun-12 02:16 AM
Check this code-

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");

I think you are trying to create database with same name which is already exist,

try to given different name which is not present.