Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- using System.Data.OleDb;
- //access db1.mdb
- //table mytable
- //a text
- //b text
- namespace WindowsFormsApplication4
- {
- public partial class Form1 : Form
- {
- //定義OLE======================================================
- //1.檔案位置
- private const string FileName = "db1.mdb";
- //2.提供者名稱
- private const string ProviderName = "Microsoft.Jet.OLEDB.4.0;";
- //3.帳號
- private const string UserId = ";";
- //4.密碼
- private const string Password = ";";
- //=============================================================
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string DataSource = Directory.GetCurrentDirectory() + "\\" + FileName;
- if (!File.Exists(DataSource))
- {
- MessageBox.Show("檔案不存在");
- return;
- }
- //連線字串
- string cs =
- "Data Source=" + DataSource + ";" +
- "Provider=" + ProviderName +
- "User Id=" + UserId +
- "Password=" + Password;
- using (OleDbConnection cn = new OleDbConnection(cs))
- {
- string qs = "SELECT b FROM mytable where a = '" + textBox1.Text.ToString() + "';";
- if (cn.State == ConnectionState.Closed)
- {
- cn.Open();
- using (OleDbCommand cmd = new OleDbCommand(qs, cn))
- {
- using (OleDbDataReader dr = cmd.ExecuteReader())
- {
- dr.Read();
- textBox2.Text = dr["b"].ToString();
- }
- }
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- label1.Text = "欄位 A";
- label2.Text = "欄位 B";
- textBox1.Text = "a01";
- textBox2.Text = "欄位 B 的值 會出現在這裡";
- }
- }
- }
Add Comment
Please, Sign In to add comment