您好,欢迎访问三七文档
//打开数据源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;}
本文标题:C#连接数据库
链接地址:https://www.777doc.com/doc-4690270 .html