 |
|
พี่พี่ครับช่วยผมอีกที นะคับผมทำได้อยู่แค่นี้อะคับ
ผมยัง Add ก็กรอกข้อมูก็สามารถ Add ข้อมูลได้เลย
Edit ก็ต้องกรอกรหัสก็จะดึงข้อมูลขึนมาและก็จะสามารถแก้ไขได้
Dele ส่วนลบก็กรอกรหัสข้อมูลขึนมาและก็คลิกปุมลบข้อมูลก็จะถูกลบทั้งแถว //// ตรงส่วนพวกนี้แหละคับที่ผมยังขาดอยู่

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Main : Form
{
OleDbConnection conn;
OleDbDataAdapter da;
DataSet ds;
DataTable dt;
DataRow dr;
OleDbCommand cmd = new OleDbCommand();
public Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connstr = "Provider=Microsoft.ACE.OLEDB.12.0; data source=C:\\person.accdb";// เชื่อต่อกับฐานข้อมูลชื่อ person.accdb
conn = new OleDbConnection();
conn.ConnectionString = connstr;
conn.Open();
da = new OleDbDataAdapter("select * from person", conn);// ส่งข้อมูล SQL ผ่านทาง conn
ds = new DataSet();// สร้างข้อมูล DataSet
da.Fill(ds, "person");
dt = ds.Tables["person"];
dataGridView1.DataSource = ds.Tables["person"];//แสงข้อมูลผ่านทาง DAtaGridView
MessageBox.Show(conn.State.ToString());
}
private void button3_add(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Open)
{
conn.Close(); // ยุติ Connection กับฐานข้อมูล
MessageBox.Show(conn.State.ToString());
}
}
private void button5_edit(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
this.BindingContext[dt].Position = 0;
}
private void button8_Click(object sender, EventArgs e)
{
this.BindingContext[dt].Position = this.BindingContext[dt].Position - 1;
}
private void button9_Click(object sender, EventArgs e)
{
this.BindingContext[dt].Position = this.BindingContext[dt].Position + 1;
}
private void button10_Click(object sender, EventArgs e)
{
this.BindingContext[dt].Position = this.BindingContext[dt].Count - 1;
}
private void button4_Click_1(object sender, EventArgs e)
{
textBox2.DataBindings.Add("Text", dt, "id");// ให้ข้อความแสดงบน textBox
textBox3.DataBindings.Add("Text", dt, "name");// ให้ข้อความแสดงบน textBox
textBox4.DataBindings.Add("Text", dt, "address");// ให้ข้อความแสดงบน textBox
textBox5.DataBindings.Add("Text", dt, "tel"); // ให้ข้อความแสดงบน textBox
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button6_dele(object sender, EventArgs e)
{
MessageBox.Show("คุณได้ทำการลบข้อมูลเสร็จเรียบร้อบแล้ว");
}
}
Tag : Ms Access, MySQL, C#
|
|
 |
 |
 |
 |
Date :
2012-09-20 21:06:03 |
By :
kurosakise |
View :
1102 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |