【Rosion L.X】ASP.NET[C#]的ACCESS資料庫操作類
//private string datapatch = ConfigurationSettings.AppSettings["acessconn"];//資料庫地址
private string datapatch = "db/global.asa";//資料庫地址
///
/// 取得dataset
///
/// 查詢語句
///
public DataSet GetDataSet(string Commandtext)
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath(datapatch);
string strCommandText = Commandtext;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection.Open();
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater.Fill(myDataset);
myConnection.Close();
return myDataset;
}
///
/// 取得表
///
/// 查詢語句
///
public DataTable GetDataTable(string Commandtext)
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath(datapatch);
string strCommandText =Commandtext;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection.Open();
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater.Fill(myDataset);
DataTable mytable = myDataset.Tables[0];
myConnection.Close();
return mytable;
}
///
/// 取得某行的某列的值
///
/// 列的名稱
/// 所屬表名
/// 表的主鍵
/// 列所屬的主鍵值
///
public string GetDataColum(string strColumnName,string strTableName,string strColumnkey,string strColumnValue)
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath(datapatch);
string strCommandText = "select "+strColumnName+","+strColumnkey+" from "+strTableName;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection.Open();
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myAdpater.Fill(myDataset);
myConnection.Close();
DataTable mytable = myDataset.Tables[0];
DataRow mydr = mytable.Rows.Find(strColumnValue);
string mydc = mydr[strColumnName].ToString();
return mydc;
}
///
/// 更新某個欄位
///
/// 要更新欄位名稱
/// 要更新的值
/// 所屬表名稱
/// 表中KEY
/// 表中KEY的值
public void UpdateColum(string strColumnName,string strValue,string strTableName,string strColumnKey,string strColumnValue)
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath(datapatch);
string strCommandText = "select "+strColumnKey+","+strColumnName+" from "+strTableName;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection.Open();
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myAdpater.Fill(myDataset,strTableName);
DataRow drFindRow = myDataset.Tables[strTableName].Rows.Find(strColumnValue);
drFindRow[strColumnName] = strValue;
myAdpater.Update(myDataset,strTableName);
myConnection.Close();
}
///
/// 新增行
///
/// 此行中的欄位集合
/// 此行中的欄位集合的對應值
/// 所屬表名稱
/// 表中主鍵
public void AddRow(string[]columns,string[]columnvalue,string strTableName,string strColumnKey)
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath(datapatch);
string strColumnCount = "";
foreach(string thiscolunmname in columns)
{
strColumnCount = thiscolunmname+","+strColumnCount;
}
string strCommandText = "select "+strColumnCount+strColumnKey+" from "+strTableName;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection.Open();
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater.Fill(myDataset,strTableName);
DataRow newrow = myDataset.Tables[strTableName].NewRow();
for(int i=0;i
string strColumnName = columns[i].ToString();
newrow[strColumnName] = columnvalue[i].ToString();
}
myDataset.Tables[strTableName].Rows.Add(newrow);
myAdpater.Update(myDataset,strTableName);
myConnection.Close();
}
///
/// 更新資料行
///
/// 要更新的列集合
/// 要更新的列集合的對應值
/// 所屬表名稱
/// 表主鍵
/// 行所屬的ID
public void updateRow(string[]columns,string[]columnvalue,string strTableName,string strColumnKey,string strColumnValue)
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath(datapatch);
string strColumnCount = "";
foreach(string thiscolunmname in columns)
{
strColumnCount = thiscolunmname+","+strColumnCount;
}
string strCommandText = "select "+strColumnCount+strColumnKey+" from "+strTableName;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection.Open();
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myAdpater.Fill(myDataset,strTableName);
DataRow updaterow = myDataset.Tables[strTableName].Rows.Find(strColumnValue);
for(int i=0;i
string strColumnName = columns[i].ToString();
updaterow[strColumnName] = columnvalue[i].ToString();
}
myAdpater.Update(myDataset,strTableName);
myConnection.Close();
}
///
/// 刪除行
///
/// 所屬表
/// 主鍵欄位名
/// 此行主鍵值
public void delectRow(string strTableName,string strColumnKey,string strColumnValue)
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath(datapatch);
string strCommandText = "select "+strColumnKey+" from "+strTableName;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection.Open();
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myAdpater.Fill(myDataset,strTableName);
DataRow newrow = myDataset.Tables[strTableName].Rows.Find(strColumnValue);
newrow.Delete();
myAdpater.Update(myDataset,strTableName);
myConnection.Close();
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-402850/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C#的Access資料庫操作 AccessHelper類C#資料庫
- ACCESS資料庫C#操作類(包含事務)資料庫C#
- C# 操作 access 資料庫C#資料庫
- C# BETA2中操作ACCESS資料庫 (轉)C#資料庫
- ASP.NET MongoDB資料庫操作類ASP.NETMongoDB資料庫
- C#:資料庫SQL操作通用類C#資料庫SQL
- C# BETA2中操作ACCESS資料庫(三) (轉)C#資料庫
- C# BETA2中操作ACCESS資料庫(二) (轉)C#資料庫
- 用ASP.NET/C#連線Access和SQL Server資料庫 (轉)ASP.NETC#SQLServer資料庫
- asp.net連線Access資料庫例子ASP.NET資料庫
- SQLite Helper類,基於.net c#的SQLite資料庫操作類SQLiteC#資料庫
- ASP.NET中防止Access資料庫下載ASP.NET資料庫
- 直接通過ADO操作Access資料庫資料庫
- PHP常用操作類實現——資料庫操作類PHP資料庫
- c#資料操作:資料庫訪問 和 檔案操作C#資料庫
- 不用JDBC:ODBC bridge直接操作Access 資料庫 (轉)JDBC資料庫
- C#操作Access的一些小結C#
- Access資料庫日常維護和Access資料庫最佳化方法資料庫
- 【侯壘 】SQL資料庫操作類SQL資料庫
- Asp.net連線Access資料庫相對路徑寫法ASP.NET資料庫
- php簡單操作mysql資料庫的類PHPMySql資料庫
- Oracle,SqlServer,Access資料庫通用訪問類設計(轉)OracleSQLServer資料庫
- .NET關於資料庫操作的類-囊括所有的操作資料庫
- 轉發:C#操作SQL Server資料庫C#SQLServer資料庫
- ASP.NET程式的優化建議資料庫操作ASP.NET優化資料庫
- 【C#】SQL資料庫助手類1.0(自用)C#SQL資料庫
- 【Python】基於pymysql的資料庫操作類PythonMySql資料庫
- 備份和恢復SQL Server資料庫+壓縮ACCESS的類(方法)SQLServer資料庫
- Access 匯入 oracle 資料庫Oracle資料庫
- PHP 連線access資料庫PHP資料庫
- java連線access資料庫Java資料庫
- .net中webform和winform連線sql server 2000資料庫的c#操作類-.NET教程,資料庫應用WebORMSQLServer資料庫C#
- ASP.NET Core使用EF Core操作MySql資料庫ASP.NETMySql資料庫
- C#快速搭建模型資料庫SQLite操作C#模型資料庫SQLite
- C#/Python/MATLAB操作PostgreSQL資料庫C#PythonMatlabSQL資料庫
- 【ASP.NET開發】ASP.NET對SQLServer的通用資料庫訪問類ASP.NETSQLServer資料庫
- C# SQLite資料庫 訪問封裝類C#SQLite資料庫封裝
- Python3資料庫操作基本類Python資料庫