//網上很多都是操作SQL SER 的
//private string datapatch = ConfigurationSettings
private string datapatch =
///
/// 取得dataset
//
/// 查詢語句
///
public DataSet GetDataSet(string Commandtext)
{
string strConnection =
string strCommandText = Commandtext;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater
myConnection
return myDataset;
}
///
/// 取得表
///
/// 查詢語句
///
public DataTable GetDataTable(string Commandtext)
{
string strConnection =
string strCommandText =Commandtext;
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater
DataTable mytable = myDataset
myConnection
return mytable;
}
///
/// 取得某行的某列的值
///
/// 列的名稱
/// 所屬表名
/// 表的主鍵
/// 列所屬的主鍵值
///
public string GetDataColum(string strColumnName
{
string strConnection =
string strCommandText =
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater
myAdpater
myConnection
DataTable mytable = myDataset
DataRow mydr = mytable
string mydc = mydr[strColumnName]
return mydc;
}
///
/// 更新某個字段
///
/// 要更新字段名稱
/// 要更新的值
/// 所屬表名稱
/// 表中KEY
/// 表中KEY的值
public void UpdateColum(string strColumnName
{
string strConnection =
string strCommandText =
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater
myAdpater
DataRow drFindRow = myDataset
drFindRow[strColumnName] = strValue;
myAdpater
myConnection
}
///
/// 添加行
///
/// 此行中的字段集合
/// 此行中的字段集合的對應值
/// 所屬表名稱
/// 表中主鍵
public void AddRow(string[]columns
{
string strConnection =
string strColumnCount =
foreach(string thiscolunmname in columns)
{
strColumnCount = thiscolunmname+
}
string strCommandText =
OleDbConnection myConnection = new OleDbConnection(strConnection);
myConnection
OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);
DataSet myDataset = new DataSet();
myAdpater
DataRow newrow = myDataset
for(int 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();
}
From:http://tw.wingwit.com/Article/program/net/201311/12662.html