using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace 資料庫連接
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public String GetSqlStr(String str)
{
return str.Replace("'", "''");
}
public void ShowPerson()
{
String connStr, selectCmd;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=PERSONDB.mdb";
selectCmd = "Select * From PERSON Order By id_no DESC";
OleDbConnection conn;
OleDbDataAdapter myAdapter;
DataSet myDataSet = new DataSet();
conn = new OleDbConnection(connStr);
conn.Open();
myAdapter = new OleDbDataAdapter(selectCmd, conn);
myAdapter.Fill(myDataSet, "PERSON");
dataGridView1.DataSource = myDataSet.Tables["PERSON"];
}
private void Form1_Load(object sender, EventArgs e)
{
ShowPerson();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string connStr, insertCmd;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=PERSONDB.mdb";
insertCmd = "Insert Into PERSON(name,id_position,tel,salary) Values('" + GetSqlStr(txtName.Text) + "','" + GetSqlStr(txtPosition.Text) + "','" + GetSqlStr(txtTel.Text) + "'," + int.Parse(txtSalary.Text) + ")";
OleDbConnection conn;
OleDbCommand cmd;
conn = new OleDbConnection(connStr);
conn.Open();
cmd = new OleDbCommand(insertCmd, conn);
cmd.ExecuteNonQuery();
conn.Close();
ShowPerson();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
String connStr, updateCmd;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=PERSONDB.mdb";
updateCmd = "update PERSON Set id_position = '" + GetSqlStr(txtPosition.Text) + "',tel='" + GetSqlStr(txtTel.Text) + "', salary =" + int.Parse(txtSalary.Text) + " Where name ='" + GetSqlStr(txtName.Text) + "'";
OleDbConnection conn;
OleDbCommand cmd;
conn = new OleDbConnection(connStr);
conn.Open();
cmd = new OleDbCommand(updateCmd, conn);
cmd.ExecuteNonQuery();
conn.Close();
ShowPerson();
}
private void btndel_Click(object sender, EventArgs e)
{
String connStr, delCmd;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Sourse=PERSONDB.mdb";
delCmd = "Delete From PERSON Where name ='" + GetSqlStr(txtName.Text) + "'";
OleDbConnection conn;
OleDbCommand cmd;
conn = new OleDbConnection(connStr);
conn.Open();
cmd = new OleDbCommand(delCmd, conn);
cmd.ExecuteNonQuery();
conn.Close();
ShowPerson();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/43113.html
標籤:C#
上一篇:用VS做GirdView的應用 真找不到哪里出錯了 求大神解救 代碼和問題如下
下一篇:如何獲取js生成的Html內容
