001.
namespace
TORServices
002.
{
003.
public
enum
EnumConnstring
004.
public
enum
EnumConnectionType
005.
{ ODBC, OLEDB, SqlClient }
006.
public
class
_DatabaseSetting
007.
{
008.
public
string
FileDatabase;
009.
public
EnumConnectionType enumConnectionType;
010.
public
EnumConnstring enumConnstring;
011.
public
string
PASS =
""
;
012.
}
013.
public
static
class
clsDatabaseServices
014.
{
015.
public
static
ArrayList DatagridToArray(DataGridView dgv,
int
column)
016.
{
017.
ArrayList r =
new
ArrayList();
018.
if
(dgv.RowCount - 1 > 0)
019.
{
020.
for
(
int
i = 0; i < dgv.RowCount - 1; i++)
021.
{
022.
r.Add(dgv[i, column].Value);
023.
}
024.
}
025.
return
r;
026.
}
027.
028.
public
static
void
TextIndexAndValue(TextBox txtIndex, TextBox txtValue,
string
_SQL,
string
_ConnectionString)
029.
{
030.
List<TORServices._DabaseSelected> lst =
new
List<TORServices._DabaseSelected>();
031.
lst = clsDatabaseServices.ReturnListIndex(_SQL, _ConnectionString, TORServices.EnumConnectionType.SqlClient);
032.
if
(lst.Count > 0)
033.
{
034.
txtIndex.Text = lst[0]._Key.ToString();
035.
txtValue.Text = lst[0]._Value;
036.
037.
}
038.
039.
}
040.
public
static
System.Windows.Forms.ComboBox DatabaseInCombobox(
string
_SQL,
string
strConn, EnumConnectionType ConnectionType)
041.
{
042.
ComboBox cmb =
new
ComboBox();
043.
cmb.DataSource = GetTableForm(_SQL, strConn, ConnectionType);
044.
return
cmb;
045.
}
046.
public
static
void
GetValueBindingSource(BindingSource bs,
string
column,
string
index)
047.
{
048.
049.
if
(index.Length > 0)
050.
{
051.
bs.Position = bs.Find(column, index);
052.
}
053.
}
054.
public
static
string
Connstring(
string
_file,EnumConnstring contype ,
string
pass =
"4410210091"
)
055.
{
056.
return
"Dsn=MS Access Database;dbq="
+ _file +
";defaultdir="
+ System.IO.Path.GetDirectoryName(_file) +
";driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;pwd="
+ pass +
";uid=admin"
;
057.
058.
}
059.
public
static
System.Data.DataTable GetTableForm(
string
_SQL,
string
strConn, EnumConnectionType ConnectionType )
060.
061.
{
062.
063.
System.Data.DataSet ds =
new
System.Data.DataSet();
064.
switch
(ConnectionType)
065.
{
066.
case
EnumConnectionType.ODBC:
067.
System.Data.Odbc.OdbcConnection conn1 =
new
System.Data.Odbc.OdbcConnection(strConn);
068.
conn1.Open();
069.
System.Data.Odbc.OdbcDataAdapter adapter1 =
new
System.Data.Odbc.OdbcDataAdapter(_SQL, conn1);
070.
adapter1.Fill(ds);
071.
break
;
072.
case
EnumConnectionType.OLEDB:
073.
System.Data.OleDb.OleDbConnection conn2 =
new
System.Data.OleDb.OleDbConnection(strConn);
074.
conn2.Open();
075.
System.Data.OleDb.OleDbDataAdapter adapter2 =
new
System.Data.OleDb.OleDbDataAdapter(_SQL, conn2);
076.
adapter2.Fill(ds);
077.
break
;
078.
case
EnumConnectionType.SqlClient:
079.
System.Data.SqlClient.SqlConnection conn3 =
new
System.Data.SqlClient.SqlConnection(strConn);
080.
conn3.Open();
081.
System.Data.SqlClient.SqlDataAdapter adapter3 =
new
System.Data.SqlClient.SqlDataAdapter(_SQL, conn3);
082.
adapter3.Fill(ds);
083.
084.
break
;
085.
}
086.
087.
return
ds.Tables[0];
088.
089.
}
090.
public
static
string
[] GetData(
string
field,
string
table,
string
strConn, EnumConnectionType ConnectionType )
091.
{
092.
DataTable dt = GetTableForm(
"Select "
+ field +
" FROM "
+ table, strConn, ConnectionType);
093.
string
[] str=
new
string
[dt.Rows.Count];
094.
for
(
int
i = 0;i<dt.Rows.Count;i++)
095.
{
096.
str[i] = dt.Rows[i][field].ToString();
097.
}
098.
return
str;
099.
100.
}
101.
public
static
DataRow GetDataRow(
string
sql ,
string
strConn, EnumConnectionType ConnectionType)
102.
{
103.
DataTable dt = GetTableForm(sql, strConn, ConnectionType);
104.
105.
return
dt.Rows[0];
106.
107.
}
108.
109.
public
static
System.Data.DataTable GetTableFormExcel(
string
_SQL,
string
FileExcel)
110.
{
111.
string
_ConnectionString = @
"Data Source="
+ FileExcel +
"; Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;"
;
112.
try
113.
{
114.
System.Data.OleDb.OleDbConnection conn =
new
System.Data.OleDb.OleDbConnection(_ConnectionString);
115.
conn.Open();
116.
string
strQuery = _SQL;
117.
System.Data.OleDb.OleDbDataAdapter adapter =
new
System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
118.
System.Data.DataSet ds =
new
System.Data.DataSet();
119.
adapter.Fill(ds);
120.
return
ds.Tables[0];
121.
}
122.
catch
{
return
null
; }
123.
124.
}
125.
126.
public
static
void
DataGridViewOpenEditFile(System.Windows.Forms.DataGridView DGV, System.Windows.Forms.DataGridViewCellEventArgs e,
int
clnOpen,
int
clnEdit,
string
PathOpen,
bool
EditFile =
false
)
127.
{
128.
129.
130.
if
(e.ColumnIndex == clnEdit)
131.
{
132.
string
s = TORServices.clsFile_Path.SelectAndSaveFile(PathOpen);
133.
134.
if
(System.IO.File.Exists(PathOpen +
"\\"
+ s))
135.
{
136.
DGV[clnOpen, e.RowIndex].Value = s;
137.
}
138.
}
139.
else
if
(e.ColumnIndex == clnOpen)
140.
{
141.
if
(System.IO.File.Exists(PathOpen +
"\\"
+ DGV[clnOpen, e.RowIndex].Value.ToString()))
142.
{
143.
TORServices.clsFile_Path.OpenFile(PathOpen +
"\\"
+ DGV[clnOpen, e.RowIndex].Value.ToString(), EditFile);
144.
}
145.
else
{ System.Windows.Forms.MessageBox.Show(
"เกิดข้อผิดพลาด ไฟล์เสียหาย"
+ Environment.NewLine +
"กรุณาตรวจสอบ"
); }
146.
}
147.
148.
149.
}
150.
}
151.
}