Based on. NET platform of Windows programming Combat (eight) database management and the realization of other auxiliary functions

Source: Internet
Author: User
Tags filter access database

This article supporting source code

Disclaimer: This series of courses is designed for beginners to get started on the practice, the purpose is to pass a complete questionnaire management system case to let the novice understand, deepen or familiar with the project development process and

Programming windows with VS2005 and C # on the. NET platform; I try to avoid or not discuss the underlying and performance optimizations throughout the course design,

So the master can completely ignore this series of courses.

This lesson will lead you to achieve database export and restore, window management and other ancillary functions. These features are not the main functions of this case system, but some auxiliary functions,

Therefore, this course is only to extract a few functions out of a simple explanation of its implementation of the principle or method, as for its design ideas here will not speak, there seems to be nothing to speak of ^_^

Let's go to the topic ...

The realization of database export and restore function

We in some systems should also have seen the function of database export and restore, do not know that everyone in use when they have to think about the principle of its implementation? In fact, the principle and logic of its implementation is very simple:

When exporting, locate the location of its database and its name, and then copy it to a new location with a file.copy () method;

and restore, exactly the opposite, that is, from the new location copy to the current system database location,

and overwrite the current system's database, and its implementation code looks like this:

/**////<summary>
Export Database
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Bakdbtoolstripmenuitem_click (object sender, EventArgs e)
{
String dbpath = @ ". \.. \database\lj_questionnairesys.mdb ";//Get the current database path, please precede the problem with the" ... \.. \, but when you publish, remove the previous. \.. \"
String _dbpath = DBPath. Substring (DBPath. LastIndexOf (' \ \ '));//Remove the name of the database
SaveFileDialog SaveFileDialog = new SaveFileDialog ();//Open dialog box for Save path
Savefiledialog.filter = "Access database file (*.mdb) |*.mdb"; To set a saved file type
Savefiledialog.title = "Please choose Saved path";//dialog box title
Savefiledialog.restoredirectory = true;//revert to the current directory State before the dialog box closes
if (savefiledialog.showdialog () = = DialogResult.OK)
{
Try
{
File.Copy (DBPath, savefiledialog.filename,true);//Copy the database file to the selected new path location
MessageBox.Show ("Export success!") Please take good care of your exported database! ^_^ "," Operation Hint ", messageboxbuttons.ok,messageboxicon.information);
}
catch (Exception)
{
MessageBox.Show ("Export failed!") Please export again! "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

}
/**////<summary>
Restore Database
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Rdbtoolstripmenuitem_click (object sender, EventArgs e)
{
if (MessageBox.Show ("Note: This operation will restore the existing database and not be recoverable, please proceed with caution!") "," Operation Hint ", MessageBoxButtons.OK, messageboxicon.information) = = DialogResult.OK)
{
String dbpath = @ ". \.. \database\lj_questionnairesys.mdb ";//Get current database path, if connection problem please add" ... \.. \, but when you publish, remove the previous. \.. \"
OpenFileDialog ofd = new OpenFileDialog ();
Ofd. Filter = "Access database file (*.mdb) |*.mdb";
Ofd. Title = "Please select the database to restore and open";
Ofd. Restoredirectory = true;
if (OFD. ShowDialog () = = DialogResult.OK)
{
Try
{
File.Copy (OFD. FileName, DBPath, true)//overwrite the current database with the selected database to be restored
MessageBox.Show ("Restore success!") Please refresh the appropriate list! "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show ("Restore failed!") Please check that the database you are restoring is correct! "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.