Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 2.22 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Data.OleDb;
  10.  
  11. //table PRODUCT
  12. //ID 自動編號,主鍵
  13. //NAME 文字
  14. //PRICE 數字
  15. //AMOUNT 數字
  16.  
  17. namespace WindowsFormsApplication1
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         //定義OLE======================================================
  22.         //1.檔案位置
  23.         private const string FileName = "db1.mdb";
  24.         //2.提供者名稱
  25.         private const string ProviderName = "Microsoft.Jet.OLEDB.4.0;";
  26.         //3.帳號
  27.         private const string UserId = ";";
  28.         //4.密碼
  29.         private const string Password = ";";
  30.         //=============================================================
  31.  
  32.         public Form1()
  33.         {
  34.             InitializeComponent();
  35.         }
  36.  
  37.         private void Form1_Load(object sender, EventArgs e)
  38.         {
  39.  
  40.         }
  41.  
  42.         private void button1_Click(object sender, EventArgs e)
  43.         {
  44.             string DataSource = Directory.GetCurrentDirectory() + "\\" + FileName;
  45.             if (!File.Exists(DataSource))
  46.             {
  47.                 MessageBox.Show("檔案不存在");
  48.                 return;
  49.             }
  50.             //連線字串
  51.             string cs =
  52.                     "Data Source=" + DataSource + ";" +
  53.                     "Provider=" + ProviderName +
  54.                     "User Id=" + UserId +
  55.                     "Password=" + Password;
  56.  
  57.  
  58.             using (OleDbConnection cn = new OleDbConnection(cs))
  59.             {
  60.                 string qs = "SELECT SUM(PRICE * AMOUNT) AS 總價 FROM PRODUCT;";
  61.                 if (cn.State == ConnectionState.Closed)
  62.                 {
  63.                     cn.Open();
  64.                     using (OleDbCommand cmd = new OleDbCommand(qs, cn))
  65.                     {
  66.                         using (OleDbDataReader dr = cmd.ExecuteReader())
  67.                         {
  68.                             dr.Read();
  69.                             label1.Text  = dr["總價"].ToString();
  70.                         }
  71.                     }
  72.                 }
  73.  
  74.  
  75.             }
  76.         }
  77.     }
  78. }