using System;
using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.OleDb;namespace AddressList
{ public partial class FrmAccessUse : Form { public FrmAccessUse() { InitializeComponent(); } #region 调用access标准方法 // using System.Data.OleDb;// using System.Data;// 连接字符串:String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";// 建立连接:OleDbConnection connection = new OleDbConnection(connectionString);// 使用OleDbCommand类来执行Sql语句:// OleDbCommand cmd = new OleDbCommand(sql, connection);// connection.Open(); // cmd.ExecuteNonQuery(); #endregion#region 连接字符串
//string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\程序书籍软件\c#程序代码\access数据库操作\addressList.mdb"; //绝对路径 string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Environment.CurrentDirectory+"\\addressList.mdb"; //相对路径#endregion
#region 查询
private void tsbQuery_Click(object sender, EventArgs e) { query(); } #endregion #region 查询语句 private void query() { try { gridView1.Columns.Clear(); OleDbConnection connet1 = new OleDbConnection(strcon); string sql = "select * from new1"; connet1.Open(); OleDbDataAdapter myadapter = new OleDbDataAdapter(); DataSet ds = new DataSet(); myadapter.SelectCommand = new OleDbCommand(sql, connet1); myadapter.Fill(ds); gridControl1.DataSource = ds.Tables[0]; connet1.Close(); gridControl1.Refresh(); } catch (Exception exp) {throw exp;
} } #endregion#region 保存
private void tsbSave_Click(object sender, EventArgs e) { Savebt(); } #endregion #region 保存语句 private void Savebt() { try { int[] rowCount = this.gridView1.GetSelectedRows(); string strSql = ""; foreach (int i in rowCount) { this.gridView1.FocusedRowHandle = -1; string strID = "", strBH = "", strMC = "", strLXID = "", strLX = "", strSM = "", strFZ = ""; strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString(); strBH = this.gridView1.GetRowCellValue(i, "帐目编号").ToString(); strMC = this.gridView1.GetRowCellValue(i, "帐目名称").ToString(); strLXID = this.gridView1.GetRowCellValue(i, "帐目类型ID").ToString(); strLX = this.gridView1.GetRowCellValue(i, "帐目类型").ToString(); strSM = this.gridView1.GetRowCellValue(i, "说明").ToString(); strFZ = this.gridView1.GetRowCellValue(i, "附注").ToString(); strSql = "Update new1 set 帐目编号='" + strBH + "', 帐目名称='" + strMC + "',帐目类型ID='" + strLXID + "',帐目类型='" + strLX + "',说明='" + strSM + "',附注='" + strFZ + "' where ZMID = " + strID + ""; //注意where处没有单引号 } OleDbConnection connet1 = new OleDbConnection(strcon); connet1.Open(); OleDbCommand cmd = new OleDbCommand(strSql, connet1); int a = cmd.ExecuteNonQuery(); connet1.Close(); if (a > 0) { MessageBox.Show("保存成功!"); } query(); } catch (Exception exp) {throw exp;
} } #endregion#region 添加一条新记录
private void tsbAddNew_Click(object sender, EventArgs e) { addnew(); } #endregion #region 添加语句 private void addnew() { try { string strSql = ""; OleDbConnection connet1 = new OleDbConnection(strcon); connet1.Open(); strSql = "insert into new1(帐目编号) values('NewValues')"; OleDbCommand cmd = new OleDbCommand(strSql, connet1); int a = cmd.ExecuteNonQuery(); connet1.Close(); if (a > 0) { query(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion#region 删除
private void tsbDelete_Click(object sender, EventArgs e) { delete(); } #endregion #region 删除语句 private void delete() { try { int[] rowCount = this.gridView1.GetSelectedRows(); string strSql = ""; foreach (int i in rowCount) { this.gridView1.FocusedRowHandle = -1; string strID = ""; strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString(); strSql = "delete from new1 where ZMID=" + strID + ""; //注意where处没有单引号 } OleDbConnection connet1 = new OleDbConnection(strcon); connet1.Open(); OleDbCommand cmd = new OleDbCommand(strSql, connet1); int a = cmd.ExecuteNonQuery(); connet1.Close(); if (a > 0) { MessageBox.Show("删除成功"); } query(); } catch (Exception ex) { MessageBox.Show(ex.Message);}
} #endregion }}
以上内容引用至