Check for MS Access database table if not exist create it

后端 未结 5 1748
失恋的感觉
失恋的感觉 2020-12-20 14:55

How do you programmatically check for MS Access database table, if not exist then create it?

5条回答
  •  没有蜡笔的小新
    2020-12-20 15:22

    Simply execute following code if table will exist it will return error other wise it will create a new one:

    try
    {
            OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + frmMain.strFilePath + "\\ConfigStructure.mdb");
            myConnection.Open();
            OleDbCommand myCommand = new OleDbCommand();
            myCommand.Connection = myConnection;
            myCommand.CommandText = "CREATE TABLE ()";
            myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();
    }
    catch(OleDbException e)
    {  
        if(e.ErrorCode == 3010 || e.ErrorCode == 3012)
        // if error then table exist do processing as required
    }
    

    Those error codes are returned if a table already exists - check here for all.

提交回复
热议问题