102 lines
2.4 KiB
C#
102 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Data.SqlClient;
|
|
using System.Data;
|
|
using System.Configuration;
|
|
|
|
/// <summary>
|
|
/// autoBindDataTable 的摘要描述
|
|
/// </summary>
|
|
public class autoBindDataTable
|
|
{
|
|
private string _strSQL = "";
|
|
private SqlConnection _objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
|
|
private SqlCommand _objCmd;
|
|
private SqlDataAdapter _objDataAdapter;
|
|
private SqlCommandBuilder _objDataCommandBuilder;
|
|
private DataTable _objDataTable = new DataTable();
|
|
|
|
public autoBindDataTable(string strSQL)
|
|
{
|
|
//
|
|
// TODO: 在這裡新增建構函式邏輯
|
|
//
|
|
_strSQL = strSQL;
|
|
|
|
try
|
|
{
|
|
_objConn.Open();
|
|
_objCmd = new SqlCommand(_strSQL, _objConn);
|
|
_objDataAdapter = new SqlDataAdapter(_objCmd);
|
|
_objDataCommandBuilder = new SqlCommandBuilder(_objDataAdapter);
|
|
_objDataAdapter.Fill(_objDataTable);
|
|
_objConn.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_objConn.Close();
|
|
throw new Exception("Bind DataTable 發生錯誤! " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public DataRow newRow
|
|
{
|
|
get
|
|
{
|
|
DataRow objRow = this._objDataTable.NewRow();
|
|
return objRow;
|
|
}
|
|
}
|
|
|
|
public void updateDataTable()
|
|
{
|
|
try
|
|
{
|
|
_objConn.Open();
|
|
_objDataAdapter.Update(_objDataTable);
|
|
_objConn.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_objConn.Close();
|
|
throw new Exception("DataTable 資料庫更新發生錯誤[" + _objDataTable.TableName + "], " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public void disConnection()
|
|
{
|
|
_objConn.Close();
|
|
_objConn.Dispose();
|
|
}
|
|
|
|
public DataRowCollection dataRows
|
|
{
|
|
get { return this._objDataTable.Rows; }
|
|
}
|
|
|
|
public DataRow[] selectedRows(string value)
|
|
{
|
|
return this._objDataTable.Select(value);
|
|
}
|
|
|
|
public int columnNumber
|
|
{
|
|
get { return this._objDataTable.Columns.Count; }
|
|
}
|
|
|
|
public string columnName(int columnIndex) {
|
|
string colName = "";
|
|
|
|
try
|
|
{
|
|
colName = this._objDataTable.Columns[columnIndex].ColumnName;
|
|
}
|
|
catch {
|
|
|
|
}
|
|
|
|
return colName;
|
|
}
|
|
} |