ASP.NET中怎么连接access数据库(2007版本,格式为.accdb)

ASP.NET中怎么连接access数据库(2007版本,格式为.accdb),第1张

<add key="ConnStr" value="Provider=Microsoft.Jet.OLEDB.12.0Data Source=|DataDirectory|\(数据库名称).mdb"/>后台调用:conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString()

//如果con对象为空则创建

if (con == null)

{

con = new OleDbConnection(conStr)

}

//如果con的状态不为打开状态,则将其Open

if (con.State != ConnectionState.Open)

{

con.Open()

}

建议这种问题先百度或google一下

我写的关于accsee的增删改查,希望对你有帮助

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

using System.Configuration

using System.IO//log需要

namespace AddressList

{

public partial class FrmAccessUse : Form

{

public FrmAccessUse()

{

InitializeComponent()

}

//access 不支持多条语句一起执行,所以不可以使用焦点值改变,生产sql语句的方法。

#region 调用access标准方法

//using System.Data.OleDb

//using System.Data

//连接字符串:String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0Data 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.0Data Source=D:\access数据库 *** 作\addressList.mdb" //方法1,绝对路径

#region 全部保存2--优化的多行保存语句用的全局数组。

string strUpdate = ""//全局变量,修改保存语句

String[] szzhujian

String[] szSQL

#endregion

#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()

#region 全部保存2--优化的多行保存语句

strUpdate = ""

szzhujian = null

szSQL = null

int j = this.gridView1.RowCount

szzhujian = new string[j]

szSQL = new string[j]

#endregion

}

catch (Exception exp)

{

throw exp

}

}

#endregion

#region 保存

private void tsbSave_Click(object sender, EventArgs e)

{

this.gridView1.FocusedRowHandle = -1 //关键语句,gridView1丢失焦点,进入值改变事件。

//Savebt()//单行保存

NewUpdate()//多行数据保存

}

#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 NewUpdate()

{

try

{

if (szzhujian != null)

{

int a = 0

OleDbConnection connet1 = new OleDbConnection(strcon)

connet1.Open()

for (int i = 0i <szzhujian.Lengthi++)

{

strUpdate = szSQL[i]

if (strUpdate != null)

{

OleDbCommand cmd = new OleDbCommand(strUpdate, connet1)

a = cmd.ExecuteNonQuery()

WriteMyLog(strUpdate)

}

}

connet1.Close()

if (a >0)

{

MessageBox.Show("保存成功!")

}

query()

}

else

{

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

#region 列前序号

//事件gridView1_CustomDrawRowIndicator

private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)

{

try

{

if (e.Info.IsRowIndicator)

{

if (e.RowHandle >= 0)

{

this.gridView1.IndicatorWidth = 40

e.Info.DisplayText = Convert.ToString(e.RowHandle + 1)

}

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message)

}

}

#endregion

#region 事件当焦点行值改变后

private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)

{

#region 全部保存--优化的多行保存语句(数组)

string strID = "", strSaveSql = ""

int[] rowCount = this.gridView1.GetSelectedRows()

foreach (int i in rowCount)

{

this.gridView1.FocusedRowHandle = -1

string 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()

strSaveSql = "Update new1 set 帐目编号='" + strBH + "', 帐目名称='" + strMC + "',帐目类型ID='" + strLXID + "',帐目类型='" + strLX + "',说明='" + strSM + "',附注='" + strFZ + "' where ZMID = " + strID + ""//注意where处没有单引号

}

if (strID != "" &&strSaveSql != "")

{

if (szzhujian[0] == null)

{

szzhujian[0] = strID

szSQL[0] = strSaveSql

}

else

{

for (int i = 0i <szzhujian.Lengthi++)

{

if (strID == szzhujian[i])

{

//return true

szSQL[i] = strSaveSql

break

}

else

{

//return false

if (i == szzhujian.Length - 1)

{

for (int k = 1k <szzhujian.Lengthk++)

{

if (szzhujian[k] == null)

{

szzhujian[k] = strID

szSQL[k] = strSaveSql

break

}

}

}

//break

}

}

}

}

#endregion

}

#endregion

#region 写log文件

public static void WriteMyLog(string ex)

{

string LOG_FOLDER = AppDomain.CurrentDomain.BaseDirectory + "Log"

try

{

//日志文件路径

string filePath = LOG_FOLDER + "\\" + DateTime.Now.ToShortDateString() + ".log"

if (!System.IO.Directory.Exists(LOG_FOLDER))//目录是否存在

{

Directory.CreateDirectory(LOG_FOLDER)

}

if (!File.Exists(filePath))//如果文件不存在

{

File.Create(filePath).Close()

}

StreamWriter sw = File.AppendText(filePath)

sw.WriteLine("-------------------------------------------------------------------------------------")

sw.WriteLine("Date:" + DateTime.Now.ToShortDateString() + " Time:" + DateTime.Now.ToShortTimeString())

sw.WriteLine(ex)

sw.Close()

}

catch (Exception exp)

{

throw exp

}

}

#endregion

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9679811.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存