Use C #'s powerful exception and error message to determine whether the data table in the database exists!
You can use ADO. Net to check whether a data table with a name exists in the database. However, most of the information I found is the method of using the COM component ADOX. I plan not to use this component .. It is impossible for such powerful software as NET 2.0 to implement this function!
After thinking hard, I finally thought about how to use its exception and error message to achieve this goal. If I need to create a database, I can also put it on my website in advance and download it to the current computer as needed. This also saves the way I used ADOX to create a database.
I will post my code below, which is a small summary for myself.
If you have any good solutions, please kindly advise.
Note: Because I am not very clear about the error information, I have not set the error identification information, and then provide the corresponding solution. Please set it by yourself.
Public partial class form1: Form
{
Public const string mdbname = "webmaildb. mdb ";
Public const string tbuser = "username ";
Public const string mdburi = "http: // localhost/webmailoutlook/download/webmaildb/2007-4-26_webmaildb. _ mdb ";
Public static string mdbpath = system. Io. Directory. getcurrentdirectory () + mdbname;
Public Boolean haslogin;
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
If (file. exists (mdbpath) = false)
{
Adminmdb adminmdb1 = new adminmdb ();
Adminmdb1.creatnewmdb (mdbpath );
Adminmdb1.creatnewtbuser (mdbpath, tbuser );
}
Login login1 = New Login ();
Login1.showdialog ();
}
Private void button#click (Object sender, eventargs E)
{
Makesuremdb ();
}
Private void makesuremdb ()
{
String strconnection = "provider = Microsoft. Jet. oledb.4.0 ;";
Strconnection + = @ "Data Source =" + system. Io. Directory. getcurrentdirectory () + "webmaildb. mdb ";
Oledbconnection objconnection = new oledbconnection (strconnection );
String oledbcommandstring = "select autoid from username ";
Oledbcommand objcommand = new oledbcommand (oledbcommandstring, objconnection );
Objconnection. open ();
Try
{
Oledbdatareader objdatareader = objcommand. executereader ();
If (objdatareader. Read ())
{
Textbox1.text + = objdatareader ["autoid"];
}
Else
{
Textbox1.text + = "no data found! ";
}
Objdatareader. Close ();
Objconnection. Close ();
}
Catch (exception exc)
{
The MessageBox. Show (EXC. Message + "\ r \ n system will automatically repair the database. "," Error: ", messageboxbuttons. OK );
Objconnection. Close ();
Textbox1.text + = "\ r \ n data table not found! \ R \ n starts to download a new database from the internet! ";
If (MessageBox. Show ("re-download the new database will clear your local database. Are you sure you want? Otherwise, cancel it! "," Query ", messageboxbuttons. okcancel, messageboxicon. Asterisk) = dialogresult. OK)
{
System. net. WebClient wclient = new system. net. WebClient ();
Wclient. downloadfile (mdburi, mdbpath );
Textbox1.text + = "\ r \ n the new database has been downloaded! The original database has been overwritten! ";
Makesuremdb ();
}
Else
{
Application. Exit ();
}
}
}
}