本文所述為C#數(shù)據(jù)采集器,并結(jié)合有數(shù)據(jù)庫操作,比較實(shí)用。讀者可以進(jìn)一步再完善一下寫成一個(gè)更加成熟的數(shù)據(jù)采集程序。
具體功能代碼如下:
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; //定義一個(gè)SqlConnection類型的公共變量My_con,用于判斷數(shù)據(jù)庫是否連接成功 public static string M_str_sqlcon = "Data Source=.;Database=CollectionEnginery;User id=sa;PWD="; StreamReader SReader; #region 建立數(shù)據(jù)庫連接 /// <summary> /// 建立數(shù)據(jù)庫連接. /// </summary> /// <returns>返回SqlConnection對(duì)象</returns> public static SqlConnection getcon() { My_con = new SqlConnection(M_str_sqlcon); //用SqlConnection對(duì)象與指定的數(shù)據(jù)庫相連接 My_con.Open(); //打開數(shù)據(jù)庫連接 return My_con; //返回SqlConnection對(duì)象的信息 } #endregion #region 創(chuàng)建DataSet對(duì)象 /// <summary> /// 創(chuàng)建一個(gè)DataSet對(duì)象 /// </summary> /// <param name="M_str_sqlstr">SQL語句</param> /// <param name="M_str_table">表名</param> /// <returns>返回DataSet對(duì)象</returns> public DataSet getDataSet(string SQLstr, string tableName) { getcon(); //打開與數(shù)據(jù)庫的連接 SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con); //創(chuàng)建一個(gè)SqlDataAdapter對(duì)象,并獲取指定數(shù)據(jù)表的信息 DataSet My_DataSet = new DataSet(); //創(chuàng)建DataSet對(duì)象 SQLda.Fill(My_DataSet, tableName); //通過SqlDataAdapter對(duì)象的Fill()方法,將數(shù)據(jù)表信息添加到DataSet對(duì)象中 con_close(); //關(guān)閉數(shù)據(jù)庫的連接 return My_DataSet; //返回DataSet對(duì)象的信息 } #endregion #region 關(guān)閉數(shù)據(jù)庫連接 /// <summary> /// 關(guān)閉于數(shù)據(jù)庫的連接. /// </summary> public void con_close() { if (My_con.State == ConnectionState.Open) //判斷是否打開與數(shù)據(jù)庫的連接 { My_con.Close(); //關(guān)閉數(shù)據(jù)庫的連接 My_con.Dispose(); //釋放My_con變量的所有空間 } } #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 = "編號(hào)"; dataGridView1.Columns[0].Width = 40; dataGridView1.Columns[1].HeaderText = "書名"; dataGridView1.Columns[1].Width = 140; dataGridView1.Columns[2].HeaderText = "條形碼"; dataGridView1.Columns[2].Width = 80; dataGridView1.Columns[3].HeaderText = "累加值"; dataGridView1.Columns[3].Width = 80; dataGridView1.Columns[4].HeaderText = "總計(jì)"; dataGridView1.Columns[4].Width = 40; } private void button1_Click(object sender, EventArgs e) { string tem_str = "";//記錄當(dāng)前行 string tem_code = "";//條形碼號(hào) string tem_mark = "";//個(gè)數(shù) string tem_s=" "; StreamReader var_SRead = new StreamReader(Application.StartupPath + "http://AddData.dat");//實(shí)例化StreamReader,并打開指定的文件 while (true)//讀取dat文件中的所有行 { tem_str = var_SRead.ReadLine();//記錄dat文件指定行的數(shù)據(jù) tem_code = tem_str.Substring(0, tem_str.IndexOf(Convert.ToChar(tem_s))).Trim();//獲取當(dāng)前行的條形碼 tem_mark = tem_str.Substring(tem_str.IndexOf(Convert.ToChar(tem_s)), tem_str.Length - tem_str.IndexOf(Convert.ToChar(tem_s))-1).Trim();//獲取當(dāng)前條形碼的個(gè)數(shù) for (int i = 0; i < dataGridView1.RowCount - 1; i++)//在dataGridView1控件中查找相應(yīng)的條形碼 { if (dataGridView1.Rows[i].Cells[2].Value.ToString().Trim() == tem_code)//如查找到 { dataGridView1.Rows[i].Cells[3].Value = tem_mark.ToString();//顯示當(dāng)前要添加的個(gè)數(shù) dataGridView1.Rows[i].Cells[4].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value) + Convert.ToInt32(tem_mark);//計(jì)算當(dāng)前條形碼的總數(shù) } } if (var_SRead.EndOfStream)//如果查詢到文件尾 break;//退出循環(huán) } var_SRead.Close();//釋放所有資源 } }}新聞熱點(diǎn)
疑難解答
圖片精選