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.OleDb; //เพื่อติดต่อฐานข้อมูลโดยใช้คำสั่ง OleDb
using Excel = Microsoft.Office.Interop.Excel; //เพื่อนำข้อมูลเข้า Excel
using System.IO;
namespace Excel
{
public partial class Welcome : Form
{
public Welcome()
{
InitializeComponent();
}
//ชนิดของฐานข้อมูลและที่อยู่ของข้อมูล
int flag = 0;
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + @"Data Source=D:\study\Database2.accdb";//เชื่อมกับฐานข้อมูล acess
//เมื่อกดปุ่ม Read
#region FunctionRead
private void but_read_Click(object sender, EventArgs e)
{
OleDbConnection Conn = new OleDbConnection(strConn);
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
if (Conn.State == ConnectionState.Open)
{
//Function for Read Data
string sql = "SELECT * FROM Table1";
OleDbCommand cmd = new OleDbCommand(sql, Conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
DataSet data = new DataSet();
adapter.Fill(data, "Tab1");
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
if (Conn.State == ConnectionState.Open)
{
//Function for Read Data
string sql = "SELECT * FROM Table1";
OleDbCommand cmd = new OleDbCommand(sql, Conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
DataSet data = new DataSet();
adapter.Fill(data, "Tab1");