Based on. NET platform of Windows programming Combat (VII.) The realization of statistical function of questionnaire (the following)

Source: Internet
Author: User
Tags bind continue count empty tostring trim access database

2. Implementation of merging statistics

Figure 7-2

From the above figure, we must have guessed the method of merging the questionnaire, yes, we do this by importing data from the outside, and the prototype of the design is this: for example, we have 20 withdrawn questionnaires, which are counted by A and B 2 staff members, each with 10 copies, b Some statistics are good, the database of its statistics export and copy to a certain, a a certain database of B, you can use the "browse" in Figure 7-2 to find a database of B, and select the need to merge the statistics of the questionnaire, the point of "confirm the merger", the system can be the merger of statistics. The code for its implementation looks like this:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using system.io;//introduces file Action class library

Namespace Lj_questionnairesystem
{
/**////<summary>
Name: Combined Questionnaire statistics category
Author:asidy
Time:2009.04.26gtf
</summary>
public partial class Surveystunite:surverywin
{
Public Surveystunite ()
{
InitializeComponent ();
}
Initializing a questionnaire list: binding questionnaires to Surveycombox controls
private void Surveystunite_load (object sender, EventArgs e)
{
String sql = "Select Id,survey_name from Lj_survey";//Query Questionnaire list
DataTable SDT = dboperate. getdatatable (SQL)//Get Query data sheet
if (SDT). Rows.Count > 0)//If the questionnaire exists, do the following
{
Surveycombox.datasource = SDT; Set the Surveycombox data source to SDT
Surveycombox.displaymember = "Survey_name"; Bind the questionnaire name to the display property of the Surveycombox list item to display the questionnaire name
Surveycombox.valuemember = "id"; Bind the ID value of the questionnaire to the actual value property of the Surveycombox list item so that the ID value of the query is retrieved when the action is made
}
}
/**////<summary>
Find database files for merged statistics
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Findfolderbt_click (object sender, EventArgs e)
{
OpenFileDialog Opf = new OpenFileDialog ()//Instantiate an open Dialog object
Opf. Filter = "Access database file (*.mdb) |*.mdb";//Set File type
Opf. title = "Select Merged database";//Set dialog box title
Opf. restoredirectory = true;//dialog box Close restore to current directory
if (Opf. ShowDialog () = = DialogResult.OK)//If the dialog box returns OK, do the following
{
Statdbfiletxt.text = Opf. FileName; Assign a lookup merged database file name to the Statdbfiletxt control
}

Opf. Dispose ();//Release the resource used by the dialog box
}
/**////<summary>
This event is raised when content in the Statdbfiletxt changes
This event is raised when a user enters or selects a merged database
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Statdbfiletxt_textchanged (object sender, EventArgs e)
{
if (StatDbFileTxt.Text.Trim ()!= "")//if the content in Statdbfiletxt is not empty, do the following, or the pop-up prompts dialog box
{
sureunitebtn.enabled = true;//Set the OK Merge button to be operational
Cancelbtn.enabled = true;
Try
{//catch exception
if (StatDbFileTxt.Text.Trim (). Substring (StatDbFileTxt.Text.LastIndexOf ('. '))!= ". mdb")
{///If the user enters or chooses a database file that is not a file with an. mdb extension, the prompt dialog box pops up and the OK Merge button is set to not be operational
MessageBox.Show ("Please enter or select the correct database path!") "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
sureunitebtn.enabled = false;//Set the OK Merge button to a non-operational state
}
Else
{
sureunitebtn.enabled = true;//Set the OK Merge button to be operational
String dbpath = @ ". \.. \database\1.mdb "; Defines the database cache name and path that is used to cache the merged database. Note: The publication is set to: @ "Database\1.mdb"
Try
{//catch exception
File.Copy (StatDbFileTxt.Text.Trim (), DBPath, True)//the database where user input or select merge statistics is cached to the path of DBPath settings
}
catch (FileNotFoundException)
{//If no database is found for user input or for merging statistics, the prompt dialog box pops up and the OK Merge button is set to not be operational
MessageBox.Show ("Please enter or select the correct database path!") "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
sureunitebtn.enabled = false;//Set the OK Merge button to a non-operational state
}
catch (Exception)
{//If other exceptions are thrown, the prompt dialog box pops up and the OK Merge button is set to not be operational
MessageBox.Show ("Import merged database Cache error!") It is possible that the system disk space is insufficient or the system has insufficient access to the installation directory, it is recommended that you install the system to a non-system disk (that is, a disk other than the operating system installation disk) try again! "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
sureunitebtn.enabled = false;//Set the OK Merge button to a non-operational state
}
}
}
catch (Exception)
{//If other exceptions are thrown, the prompt dialog box pops up and the OK Merge button is set to not be operational
MessageBox.Show ("Please enter or select the correct database path!") "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
sureunitebtn.enabled = false;//Set the OK Merge button to a non-operational state
}
}
}

Dboperate dboperate = new Dboperate ();//Instantiate a database action object to manipulate the database
/**////<summary>
This event is raised when the user clicks on the "OK Merge" button
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Sureunitebtn_click (object sender, EventArgs e)
{
if (allstatchbox.checked)//If all merges is selected, do the following
{
DataTable Surveydt = dboperate. GetTable ("SELECT ID from Lj_survey"); Get the corresponding Questionnaire data table in the database where the user chooses to import the merged statistics
if (Surveydt. Rows.Count > 0)//If there is a questionnaire, do the following
{
Sureunitebtn.text = "Merging ...";
sureunitebtn.enabled = false;
cancelbtn.enabled = false;
String rsql = "", Upsql = ""; Defines two strings, which are used to store SQL statements
DataTable rdt;//defines a data table that is used to store topic options under the appropriate questionnaires.
int JJJ = 0;//used to determine if the merge succeeded
for (int si = 0; si < Surveydt. Rows.Count; si++)//circular query for each questionnaire
{
The option Query statement for the topic under the corresponding questionnaire in the database where users import the merged statistics is deposited in Rsql
Rsql = "Select Result_sid,result_tid,result_content,result_count from Lj_result Where result_sid=" + Convert.ToInt32 ( Surveydt. Rows[si][0]);
Rdt = Dboperate. GetTable (Rsql)///Get the option datasheet for the topic under the corresponding questionnaire in the database where the user chooses to import the merged statistics
int JJ = 0; Used to determine whether a merge operation has been performed
if (Rdt. Rows.Count > 0)//If the datasheet exists for this option, do the following
{
Probar.visible = true; Set progress bar to visible
Probar.minimum = 0; Set the start value of the progress bar to 0
Probar.maximum = Rdt. Rows.Count; Set the maximum for a progress bar is the total number of rows in the option datasheet
for (int II = 0; II < RDT. Rows.Count; ii++)///update each corresponding data into an existing database
{
Store updated statements for corresponding items in an existing database in Upsql
Upsql = "Update lj_result Set result_count=result_count+" + Convert.ToInt32 (Rdt. ROWS[II][3]) + "Where result_sid=" + Convert.ToInt32 (Rdt. Rows[ii][0]) + "and result_tid=" + Convert.ToInt32 (Rdt. ROWS[II][1]) + "and result_content= '" + Rdt. ROWS[II][2]. ToString () + "'";
if (dboperate. Excuteintsql (Upsql) > 0)
{
jj++;//If the update succeeds, add 1
}
Probar.value = II + 1; The current value of the progress bar also adds 1
}
if (JJ > 0)//If the update succeeds, do the following: Continue to update the statistics of the corresponding questionnaire
{
Get statistics of corresponding questionnaires in the database in which users import merged statistics
int surveycount = Dboperate. Excueteintsql ("Select Survey_count from Lj_survey Where id=" + Convert.ToInt32 (Surveydt. Rows[si][0]));
Add the statistics of the corresponding questionnaires in the existing database and import the statistics of the corresponding questionnaires in the consolidated statistical database and perform the update operation
string sSQL = "Update lj_survey Set survey_count=survey_count+" + surveycount + "Where id=" + Convert.ToInt32 (Surveydt. Rows[si][0]);
Dboperate. Excutesql (sSQL); Perform an update operation
}
}
JJJ = JJJ + jj;//The data that determines whether the merge succeeds or not, and the number of updated bars to determine if the update was successful
}
if (jjj > 0)//If greater than 0, the update succeeds, do the following; otherwise pop-up merge failure prompt dialog box
{
Sureunitebtn.text = "merged successfully";
Cancelbtn.enabled = true;
MessageBox.Show ("Merge succeeded, please refresh the list!") "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
Try
{//Capture Delete exception
File.delete (@ "...) \.. \database\1.mdb ");//delete the merged cache database. Note: The release is set to: @" Database\1.mdb "
}
catch (Exception)///If an exception occurs, skip
{
}
This. Close ()//Closes the current window
}
Else
{
Sureunitebtn.text = "Merge Failed";
Cancelbtn.enabled = true;
MessageBox.Show (merge failed, please check that the imported merged database is correct!) "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Else
{//If no questionnaire exists, the pop-up prompts dialog box
Sureunitebtn.text = "Determination of mergers";
Sureunitebtn.enabled = true;
Cancelbtn.enabled = true;
MessageBox.Show ("There are no questionnaires in the imported merged database!") Please check the imported merged database! "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
This. Close ();
}
}
Else
{//If you did not select Merge all, do the following
int Surveyid = Convert.ToInt32 (Surveycombox.selectedvalue); Gets the questionnaire ID for the currently selected merge
if (StatDbFileTxt.Text.Trim ()!= "")//if the user enters or selects a database that imports the merge statistics is not empty, do the following; otherwise pop-up prompt box
{
The option query statement for the questions under the questionnaire in the database where users import the merged statistics is stored in SQL
String sql = "Select Result_sid,result_tid,result_content,result_count from Lj_result Where result_sid=" + Surveyid;
DataTable dt = dboperate. GetTable (SQL)///Get the option datasheet for the topic under the corresponding questionnaire in the database where the user chooses to import the merged statistics

if (dt. Rows.Count > 0)//If this option datasheet is not empty, do the following; otherwise pop-up prompt box
{
Sureunitebtn.text = "Merging ...";
sureunitebtn.enabled = false;
cancelbtn.enabled = false;
String tSql = "";
Int J = 0;//used to determine whether an update operation was performed
Probar.visible = true; Set progress bar to visible
Probar.minimum = 0;//Sets the start value of the progress bar to 0
int cout = dt. Total number of rows in the rows.count;//option datasheet
Probar.maximum = cout;//The maximum of the progress bar is the total number of rows in the option datasheet
for (int i = 0; i < cout i++)////update each corresponding data into an existing database
{
Store updated statements for corresponding items in an existing database in Upsql
TSQL = "Update lj_result Set result_count=result_count+" + convert.toint32 (dt. ROWS[I][3]) + "Where result_sid=" + convert.toint32 (dt. Rows[i][0]) + "and result_tid=" + convert.toint32 (dt. ROWS[I][1]) + "and result_content= '" + dt. ROWS[I][2]. ToString () + "'";
if (dboperate. Excuteintsql (TSQL) > 0)
{
j++;//If the update succeeds, add 1
}
Probar.value = i + 1; The current value of the progress bar also adds 1
}
if (J > 0)//If the update succeeds, do the following: Continue to update the statistics of the corresponding questionnaire; otherwise pop-up merge failure prompt dialog box
{
Sureunitebtn.text = "merged successfully";
Cancelbtn.enabled = true;
Get statistics of corresponding questionnaires in the database in which users import merged statistics
int surveycount = Dboperate. Excueteintsql ("Select Survey_count from Lj_survey Where id=" + Surveyid);
Add the statistics of the corresponding questionnaires in the existing database and import the statistics of the corresponding questionnaires in the consolidated statistical database and perform the update operation
string sSQL = "Update lj_survey Set survey_count=survey_count+" + surveycount + "Where id=" + Surveyid;
Dboperate. Excutesql (sSQL);//Perform the update operation
MessageBox.Show ("Merge succeeded, please refresh the list!") "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
Try
{//Capture Delete exception
File.delete (@ "...) \.. \database\1.mdb ");//delete the merged cache database. Note: The release is set to: @" Database\1.mdb "
}
catch (Exception)///If an exception occurs, skip
{
}
This. Close ()//Closes the current window
}
Else
{
Sureunitebtn.text = "Merge Failed";
Cancelbtn.enabled = true;
MessageBox.Show (merge failed, please check that the imported merged database is correct!) "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Else
{
Sureunitebtn.text = "Determination of mergers";
Sureunitebtn.enabled = true;
Cancelbtn.enabled = true;
MessageBox.Show ("did not find the corresponding questionnaire, please check whether the imported merged database contains a corresponding questionnaire!") "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
This. Close ();
}
}
Else
{
Sureunitebtn.text = "Determination of mergers";
Sureunitebtn.enabled = true;
Cancelbtn.enabled = true;
MessageBox.Show ("Please enter or select the database path to import the merge!") "," Operation Hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
/**////<summary>
This event is raised when the user clicks the "Merge all" option
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Allstatchbox_checkedchanged (object sender, EventArgs e)
{
If "All merges" is selected, no more questionnaires can be selected;
if (allstatchbox.checked)
{
surveycombox.enabled = false;
}
Else
{
Surveycombox.enabled = true;
}
}
}
}

OK, this course is first here, if there are other unclear or confused place, please note below, I will do my best to give answers, I hope to bring a little help! Thank you for your support ...

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.