C#连接数据库

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//打开数据源publicOleDbConnectionDBOpen(){stringstrConnection=Provider=Microsoft.Jet.OleDb.4.0;DataSource=;strConnection+=@Application.StartupPath+\\SMS.mdb;OleDbConnectionConn=newOleDbConnection(strConnection);Conn.Open();returnConn;}//关闭数据源连接publicvoidDBClose(OleDbConnectionConn){Conn.Close();Conn.Dispose();}//读数据库中的表publicOleDbDataReaderDBRead(OleDbConnectionConn,stringstr_select){OleDbCommandComm=newOleDbCommand(str_select,Conn);try{OleDbDataReaderReader=Comm.ExecuteReader();returnReader;}catch(Exceptionee){MessageBox.Show(ee.ToString());returnnull;}}//插入表publicBooleanInsertTable(OleDbConnectionConn,StringinsertStr){try{OleDbCommandComm=newOleDbCommand(insertStr,Conn);Comm.ExecuteNonQuery();MessageBox.Show(添加成功,成功,MessageBoxButtons.OK,MessageBoxIcon.Information);}catch(Exceptionee){MessageBox.Show(ee.ToString());returnfalse;}finally{Conn.Close();}returntrue;}//插入到Access表publicBooleanInsertAccessTable(OleDbConnectionConn,StringinsertStr){try{OleDbCommandComm=newOleDbCommand(insertStr,Conn);Comm.ExecuteNonQuery();}catch(Exceptionee){Conn.Close();returnfalse;}finally{Conn.Close();}returntrue;}//删除表publicBooleanDeleteRecords(OleDbConnectionConn,StringdeletStr){try{OleDbCommandComm=newOleDbCommand(deletStr,Conn);Comm.ExecuteNonQuery();MessageBox.Show(删除成功!,删除,MessageBoxButtons.OK,MessageBoxIcon.Information);}catch(Exceptione){returnfalse;}finally{Conn.Close();}returntrue;}//更新记录publicBooleanUpdateRecords(OleDbConnectionConn,StringupdataStr){try{OleDbCommandComm=newOleDbCommand(updataStr,Conn);Comm.ExecuteNonQuery();MessageBox.Show(修改成功!,修改,MessageBoxButtons.OK,MessageBoxIcon.Information);}catch(Exceptioneee){returnfalse;}finally{Conn.Close();}returntrue;}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功