C#結(jié)合數(shù)據(jù)庫的數(shù)據(jù)采集器示例
更新時間:2014年07月26日 12:24:13 投稿:shichen2014
這篇文章主要介紹了C#結(jié)合數(shù)據(jù)庫的數(shù)據(jù)采集器,功能比較實用,需要的朋友可以參考下
本文所述為C#數(shù)據(jù)采集器,并結(jié)合有數(shù)據(jù)庫操作,比較實用。讀者可以進一步再完善一下寫成一個更加成熟的數(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; //定義一個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對象</returns> public static SqlConnection getcon() { My_con = new SqlConnection(M_str_sqlcon); //用SqlConnection對象與指定的數(shù)據(jù)庫相連接 My_con.Open(); //打開數(shù)據(jù)庫連接 return My_con; //返回SqlConnection對象的信息 } #endregion #region 創(chuàng)建DataSet對象 /// <summary> /// 創(chuàng)建一個DataSet對象 /// </summary> /// <param name="M_str_sqlstr">SQL語句</param> /// <param name="M_str_table">表名</param> /// <returns>返回DataSet對象</returns> public DataSet getDataSet(string SQLstr, string tableName) { getcon(); //打開與數(shù)據(jù)庫的連接 SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con); //創(chuàng)建一個SqlDataAdapter對象,并獲取指定數(shù)據(jù)表的信息 DataSet My_DataSet = new DataSet(); //創(chuàng)建DataSet對象 SQLda.Fill(My_DataSet, tableName); //通過SqlDataAdapter對象的Fill()方法,將數(shù)據(jù)表信息添加到DataSet對象中 con_close(); //關(guān)閉數(shù)據(jù)庫的連接 return My_DataSet; //返回DataSet對象的信息 } #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 = "編號"; 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 = "總計"; dataGridView1.Columns[4].Width = 40; } private void button1_Click(object sender, EventArgs e) { string tem_str = "";//記錄當前行 string tem_code = "";//條形碼號 string tem_mark = "";//個數(shù) string tem_s=" "; StreamReader var_SRead = new StreamReader(Application.StartupPath + "\\AddData.dat");//實例化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();//獲取當前行的條形碼 tem_mark = tem_str.Substring(tem_str.IndexOf(Convert.ToChar(tem_s)), tem_str.Length - tem_str.IndexOf(Convert.ToChar(tem_s))-1).Trim();//獲取當前條形碼的個數(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();//顯示當前要添加的個數(shù) dataGridView1.Rows[i].Cells[4].Value = Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value) + Convert.ToInt32(tem_mark);//計算當前條形碼的總數(shù) } } if (var_SRead.EndOfStream)//如果查詢到文件尾 break;//退出循環(huán) } var_SRead.Close();//釋放所有資源 } } }
相關(guān)文章
一文詳解C#中數(shù)組、鏈表、Hash的優(yōu)缺點
在 C# 中,數(shù)組(Array)、鏈表(LinkedList)和哈希表(Hash)是常用的數(shù)據(jù)結(jié)構(gòu),每種都有其自身的優(yōu)缺點,本文將通過代碼示例給大家詳細的介紹一下,需要的朋友可以參考下2024-02-02C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為Text
這篇文章主要介紹了C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為Text,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08