Guest User

Untitled

a guest
Oct 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  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.  
  9. using System.IO;
  10. using System.Data.OleDb;
  11.  
  12. //access db1.mdb
  13. //table mytable
  14. //a text
  15. //b text
  16.  
  17.  
  18. namespace WindowsFormsApplication4
  19. {
  20. public partial class Form1 : Form
  21. {
  22.  
  23. //定義OLE======================================================
  24. //1.檔案位置
  25. private const string FileName = "db1.mdb";
  26. //2.提供者名稱
  27. private const string ProviderName = "Microsoft.Jet.OLEDB.4.0;";
  28. //3.帳號
  29. private const string UserId = ";";
  30. //4.密碼
  31. private const string Password = ";";
  32. //=============================================================
  33.  
  34. public Form1()
  35. {
  36. InitializeComponent();
  37. }
  38.  
  39. private void button1_Click(object sender, EventArgs e)
  40. {
  41. string DataSource = Directory.GetCurrentDirectory() + "\\" + FileName;
  42. if (!File.Exists(DataSource))
  43. {
  44. MessageBox.Show("檔案不存在");
  45. return;
  46. }
  47. //連線字串
  48. string cs =
  49. "Data Source=" + DataSource + ";" +
  50. "Provider=" + ProviderName +
  51. "User Id=" + UserId +
  52. "Password=" + Password;
  53.  
  54.  
  55. using (OleDbConnection cn = new OleDbConnection(cs))
  56. {
  57. string qs = "SELECT b FROM mytable where a = '" + textBox1.Text.ToString() + "';";
  58. if (cn.State == ConnectionState.Closed)
  59. {
  60. cn.Open();
  61. using (OleDbCommand cmd = new OleDbCommand(qs, cn))
  62. {
  63. using (OleDbDataReader dr = cmd.ExecuteReader())
  64. {
  65. dr.Read();
  66. textBox2.Text = dr["b"].ToString();
  67. }
  68. }
  69. }
  70.  
  71.  
  72. }
  73. }
  74.  
  75. private void Form1_Load(object sender, EventArgs e)
  76. {
  77. label1.Text = "欄位 A";
  78. label2.Text = "欄位 B";
  79. textBox1.Text = "a01";
  80. textBox2.Text = "欄位 B 的值 會出現在這裡";
  81.  
  82. }
  83. }
  84. }
Add Comment
Please, Sign In to add comment