C combined with database data collector example

  • 2020-06-23 01:48:33
  • OfStack

The C# data collector described in this paper, combined with database operation, is more practical. The reader can take one step and refine the next to write a more mature data acquisition program.

The specific function code is as follows:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace CollectionEnginery
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    public static SqlConnection My_con; // define 1 a SqlConnection Public variable of type My_con Is used to determine whether the database is successfully connected 
    public static string M_str_sqlcon = "Data Source=.;Database=CollectionEnginery;User id=sa;PWD=";
    StreamReader SReader;
    #region  Establishing a database connection 
    /// <summary>
    ///  Establishing a database connection .
    /// </summary>
    /// <returns> return SqlConnection object </returns>
    public static SqlConnection getcon()
    {
      My_con = new SqlConnection(M_str_sqlcon);  // with SqlConnection Object to connect to the specified database 
      My_con.Open(); // Open database connection 
      return My_con; // return SqlConnection Object information 
    }
    #endregion
    #region  create DataSet object 
    /// <summary>
    ///  create 1 a DataSet object 
    /// </summary>
    /// <param name="M_str_sqlstr">SQL statements </param>
    /// <param name="M_str_table"> The name of the table </param>
    /// <returns> return DataSet object </returns>
    public DataSet getDataSet(string SQLstr, string tableName)
    {
      getcon();  // Open the connection to the database 
      SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con); // create 1 a SqlDataAdapter Object and gets information about the specified data table 
      DataSet My_DataSet = new DataSet(); // create DataSet object 
      SQLda.Fill(My_DataSet, tableName); // through SqlDataAdapter The object's Fill() Method to add data table information to DataSet In the object 
      con_close();  // Close the connection to the database 
      return My_DataSet; // return DataSet Object information 
    }
    #endregion

    #region  Close the database connection 
    /// <summary>
    ///  Close the connection to the database .
    /// </summary>
    public void con_close()
    {
      if (My_con.State == ConnectionState.Open)  // Determines whether the connection to the database is open 
      {
        My_con.Close();  // Close the connection to the database 
        My_con.Dispose();  // The release of My_con All of the Spaces of the variable 
      }
    }
    #endregion

    private void Form1_Load(object sender, EventArgs e)
    {
      DataSet dataSet = new DataSet();
      dataSet = getDataSet("select * from tb_Collection", "tb_Collection");
      dataGridView1.DataSource = dataSet.Tables[0];
      dataGridView1.Columns[0].HeaderText = " Serial number ";
      dataGridView1.Columns[0].Width = 40;
      dataGridView1.Columns[1].HeaderText = " Title: ";
      dataGridView1.Columns[1].Width = 140;
      dataGridView1.Columns[2].HeaderText = " Bar code ";
      dataGridView1.Columns[2].Width = 80;
      dataGridView1.Columns[3].HeaderText = " Accumulated value ";
      dataGridView1.Columns[3].Width = 80;
      dataGridView1.Columns[4].HeaderText = " A total of ";
      dataGridView1.Columns[4].Width = 40;
    }
    private void button1_Click(object sender, EventArgs e)
    {
      string tem_str = "";// Record current row 
      string tem_code = "";// Bar code no. 
      string tem_mark = "";// The number of 
      string tem_s=" ";
      StreamReader var_SRead = new StreamReader(Application.StartupPath + "\\AddData.dat");// instantiation StreamReader , and open the specified file 
      while (true)// read dat All the lines in the file 
      {
        tem_str = var_SRead.ReadLine();// record dat The file specifies the row data 
        tem_code = tem_str.Substring(0, tem_str.IndexOf(Convert.ToChar(tem_s))).Trim();// Gets the bar code of the current row 
        tem_mark = tem_str.Substring(tem_str.IndexOf(Convert.ToChar(tem_s)), tem_str.Length - tem_str.IndexOf(Convert.ToChar(tem_s))-1).Trim();// Gets the current number of bar codes 
        for (int i = 0; i < dataGridView1.RowCount - 1; i++)// in dataGridView1 Control to find the corresponding barcode 
        {
          if (dataGridView1.Rows[i].Cells[2].Value.ToString().Trim() == tem_code)// Such as find 
          {
            dataGridView1.Rows[i].Cells[3].Value = tem_mark.ToString();// Displays the current number to add 
            dataGridView1.Rows[i].Cells[4].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value) + Convert.ToInt32(tem_mark);// Calculate the total number of current barcodes 
          }
        }
        if (var_SRead.EndOfStream)// If query to end of file 
          break;// Exit the loop 
      }
      var_SRead.Close();// Release all resources 
    }
  }
}


Related articles: