2011-01-25 11 views
8

एमएस एक्सेस डेटाबेस तालिका के लिए प्रोग्रामेटिक रूप से जांच कैसे करें, यदि मौजूद नहीं है तो इसे बनाएं? यदि तालिका उपलब्ध नहीं होगा यह अन्य बुद्धिमान त्रुटि लौटाएगायदि मौजूद नहीं है तो एमएस एक्सेस डेटाबेस तालिका के लिए जांचें

उत्तर

7

आप निम्नलिखित कोड निष्पादित यह एक नया बना देगा:

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 <yourtable name>(<columns>)"; 
     myCommand.ExecuteNonQuery(); 
     myCommand.Connection.Close(); 
} 
catch(OleDbException e) 
{ 
    if(e.ErrorCode == 3010 || e.ErrorCode == 3012) 
    // if error then table exist do processing as required 
} 

उन त्रुटि कोड लौटा दिया जाता है, तो एक मेज पहले से मौजूद है - सभी के लिए check here

12

हालांकि तालिका तालिका नाम किसी विशिष्ट तालिका की जांच करने के लिए आप फिर से सक्रिय कर सकते हैं। तालिका नाम प्राप्त करने के लिए नीचे दिए गए कोड को देखें।

 string connectionstring = "Your connection string"; 
     string[] restrictionValues = new string[4]{null,null,null,"TABLE"}; 
     OleDbConnection oleDbCon = new OleDbConnection(connectionString); 
     List<string> tableNames = new List<string>(); 

     try 
     { 
      oleDbCon.Open(); 
      DataTable schemaInformation = oleDbCon.GetSchema("Tables", restrictionValues); 

      foreach (DataRow row in schemaInformation.Rows) 
      { 
       tableNames.Add(row.ItemArray[2].ToString()); 
      } 
     } 
     finally 
     { 
      oleDbCon.Close(); 
     }   
+0

+1, बस एक त्रुटि को पकड़ने की तुलना में अधिक सुंदर है। कृपया 'पकड़ {फेंक दें; } ', हालांकि: यह एक एनओयूपी है। – Heinzi

1

पूर्णता के लिए, मैं आपको बताएंगे कि एक समय पहले मैं एक TableExists() function within Access अप कोडिंग के 4 अलग अलग तरीकों से तैनात। संस्करण जो MSysObjects पर SQL SELECT चलाता है, बाहरी एक्सेस से काम करेगा, हालांकि कुछ संदर्भों में, आपको सुरक्षा त्रुटि मिल सकती है (क्योंकि आपको जेट/एसीई सिस्टम टेबल तक पहुंचने की अनुमति नहीं है)।

8

जांच करने के लिए एक मेज से मौजूद है, अगर आप इस तरह DbConnection विस्तार कर सकते हैं:

public static class DbConnectionExtensions 
{ 
    public static bool TableExists(this DbConnection conn, string table) 
    { 
     conn.open(); 
     var exists = conn.GetSchema("Tables", new string[4] { null, null, table, "TABLE" }).Rows.Count > 0; 
     conn.close(); 
     return exists; 
    } 
} 

तो फिर तुम OleDbConnection, SQLiteConnection या SqlConnection की तरह किसी भी व्युत्पन्न वर्ग में TableExists कॉल कर सकते हैं।

1

ऐसा करने के लिए एक आसान तरीका

public bool CheckTableExistance(string TableName) 
    { 
     // Variable to return that defines if the table exists or not. 
     bool TableExists = false; 

     // Try the database logic 
     try 
     { 
      // Make the Database Connection 
      ConnectAt(); 

      // Get the datatable information 
      DataTable dt = _cnn.GetSchema("Tables"); 

      // Loop throw the rows in the datatable 
      foreach (DataRow row in dt.Rows) 
      { 
       // If we have a table name match, make our return true 
       // and break the looop 
       if (row.ItemArray[2].ToString() == TableName) 
       { 
        TableExists = true; 
        break; 
       } 
      } 

      //close database connections! 
      Disconnect(); 
      return TableExists; 
     } 
     catch (Exception e) 
     { 
      // Handle your ERRORS! 
      return false; 
     } 
    } 
संबंधित मुद्दे