001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.Linq;
004.
using
System.Text;
005.
using
System.Data;
006.
007.
namespace
TORServices.DatabaseTor
008.
{
009.
010.
public
static
class
Databaseconnection
011.
{
012.
013.
/// <summary> ตรวจสอบ ข้อความว่าอยู่ในรูปแบบ IP หรือไม่
014.
/// <para>ตัวอย่าง เช่น </para>
015.
/// <para> "192.168.23.55".CheckIP()== True </para>
016.
/// <para>อ้างอิงจาก TOR@ChemCoM </para>
017.
/// </summary>
018.
public
static
bool
CheckIP(
this
string
input)
019.
{
020.
System.Text.RegularExpressions.Regex rex =
new
System.Text.RegularExpressions.Regex(@
"(^\d{1,}\.\d{1,}\.\d{1,}\.\d{1,}\z)"
);
021.
return
rex.IsMatch(input);
022.
}
023.
#region _ODBC
024.
025.
/// <summary>ODBC
026.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
027.
/// <para>"Dsn=DsnName;Uid=UserName;Pwd=Secret;"</para>
029.
/// </summary>
030.
public
static
System.Data.Odbc.OdbcConnection OdbcConnection(
string
connectionString =
""
)
031.
{
032.
System.Data.Odbc.OdbcConnection con =
new
System.Data.Odbc.OdbcConnection(connectionString);
033.
try
{con.Open();
return
con;}
catch
(Exception ex){
throw
ex;}
034.
}
035.
/// <summary>ODBC DSN:Microsoft SQL Server,Oracle,MySql
036.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
037.
/// <para>"Dsn=DsnName;Uid=UserName;Pwd=Secret;"</para>
039.
/// </summary>
040.
public
static
System.Data.Odbc.OdbcConnection OdbcConnection(
string
DsnName,
string
UserName =
""
,
string
Pwd =
""
)
041.
{
042.
return
OdbcConnection(
"Dsn="
+ DsnName +
";Uid="
+UserName+
";Pwd="
+ Pwd +
";"
);
043.
}
044.
/// <summary>ODBC DSN:Microsoft Access
045.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
046.
/// <para>"Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\myPath\myDb.mdb;Uid=Admin;Pwd=;"</para>
048.
/// </summary>
049.
public
static
System.Data.Odbc.OdbcConnection OdbcConnection(
string
Filename,
string
Pwd =
""
)
050.
{
051.
return
OdbcConnection(
"Driver={Microsoft Access Driver (*.mdb)};Dbq="
+Filename+
";Uid=Admin;Pwd="
+Pwd+
";"
);
052.
}
053.
054.
#endregion
055.
056.
#region _OleDb
057.
/// <summary>OleDb
058.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
059.
/// <para>"Driver=SQLOLEDB;Data Source=ServerName;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"</para>
061.
/// </summary>
062.
public
static
System.Data.OleDb.OleDbConnection OleDbConnection(
string
connectionString =
""
)
063.
{
064.
System.Data.OleDb.OleDbConnection con =
new
System.Data.OleDb.OleDbConnection(connectionString);
065.
try
{con.Open();
return
con;}
catch
(Exception ex){
throw
ex;}
066.
}
067.
/// <summary>OleDb -- Standard Connection:Microsoft SQL Server
068.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
069.
/// <para>"Driver=SQLOLEDB;Data Source=ServerName;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"</para>
071.
/// </summary>
072.
public
static
System.Data.OleDb.OleDbConnection OleDbConnection(
string
DataSource,
string
DataBaseName,
string
UserName,
string
Pwd =
""
)
073.
{
074.
return
OleDbConnection(
"Driver=SQLOLEDB;Data Source="
+ DataSource +
";Initial Catalog="
+ DataBaseName +
";User id="
+ UserName +
";Password="
+ Pwd +
";"
);
075.
}
076.
/// <summary>OleDb -- via IP Address:Microsoft SQL Server
077.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
078.
/// <para>"Driver=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"</para>
080.
/// </summary>
081.
public
static
System.Data.OleDb.OleDbConnection OleDbConnection(
string
DataSource,
string
IP ,
string
DataBaseName,
string
UserName,
string
Pwd =
""
,
string
port =
"1433"
)
082.
{
083.
return
(!IP.CheckIP()) ?
null
: OleDbConnection(
"Driver=SQLOLEDB;Data Source="
+ IP +
","
+ port +
";Initial Catalog="
+ DataBaseName +
";User id="
+ UserName +
";Password="
+ Pwd +
";"
);
084.
}
085.
/// <summary>OleDb with MS Jet -- Standard Security
086.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
087.
/// <para>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mypath\myDb.mdb;User id=admin;Password="</para>
089.
/// </summary>
090.
public
static
System.Data.OleDb.OleDbConnection OleDbConnection(
string
Filename,
string
Pwd =
""
)
091.
{
092.
return
OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+Filename+
";User id=admin;"
+
";Password="
+ Pwd +
";"
);
093.
}
094.
#endregion
095.
096.
#region _SqlClient
097.
/// <summary>SqlClient
098.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
099.
/// <para>"Data Source=ServerName;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"</para>
101.
/// </summary>
102.
public
static
System.Data.SqlClient.SqlConnection SqlConnection(
string
connectionString =
""
)
103.
{
104.
System.Data.SqlClient.SqlConnection con =
new
System.Data.SqlClient.SqlConnection(connectionString);
105.
try
106.
{
107.
con.Open();
108.
return
con;
109.
}
110.
catch
(Exception ex)
111.
{
112.
throw
ex;
113.
}
114.
}
115.
/// <summary>.NET DataProvider -- Standard Connection:Microsoft SQL Server
116.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
117.
/// <para>"Data Source=ServerName;Initial Catalog=DataBaseName;Integrated Security=SSPI;"</para>
119.
/// </summary>
120.
public
static
System.Data.SqlClient.SqlConnection SqlConnection(
string
DataSource,
string
DataBaseName,
string
UserName,
string
Pwd =
""
)
121.
{
122.
return
SqlConnection(
"Data Source="
+ DataSource +
";"
+
"Initial Catalog="
+ DataBaseName +
";"
+
"User id="
+ UserName +
";"
+
"Password="
+ Pwd +
";"
);
123.
124.
}
125.
/// <summary>.NET DataProvider -- via IP Address:Microsoft SQL Server
126.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
127.
/// <para>"Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=DataBaseName;User Id=UserName;Password=Secret;"</para>
129.
/// </summary>
130.
public
static
System.Data.SqlClient.SqlConnection SqlConnection(
string
IP,
string
DataBaseName,
string
UserName,
string
Pwd =
""
,
string
port =
"1433"
)
131.
{
132.
133.
return
(!IP.CheckIP())?
null
: SqlConnection(
"Network Library=DBMSSOCN;Data Source="
+IP+
","
+port+
";Initial Catalog="
+DataBaseName+
";User Id="
+UserName+
";Password="
+Pwd+
";"
);
134.
135.
}
136.
/// <summary>Microsoft Sql Express.NET Data Provider -- Default Relative Path -- Standard Connection:Microsoft SQL Server
137.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
138.
/// <para>"Data Source=.\SQLExpress;User Instance=true;User Id=UserName;Password=Secret;AttachDbFilename=|DataDirectory|DataBaseName.mdf;"</para>
140.
/// </summary>
141.
public
static
System.Data.SqlClient.SqlConnection SqlConnection(
string
DataBaseName,
string
UserName,
string
Pwd =
""
)
142.
{
143.
return
SqlConnection(
"Data Source=.\\SQLExpress;User Instance=true;User Id="
+UserName+
";Password="
+ Pwd+
";AttachDbFilename="
+DataBaseName+
";"
);
144.
}
145.
#endregion
146.
147.
#region _OracleClient
148.
/// <summary>OracleClient
149.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
150.
/// <para>"Data Source=ServerName;User id=UserName;Password=Secret;"</para>
152.
/// </summary>
153.
public
static
System.Data.OracleClient.OracleConnection OracleConnection(
string
connectionString =
""
)
154.
{
155.
System.Data.OracleClient.OracleConnection con =
new
System.Data.OracleClient.OracleConnection(connectionString);
156.
try
157.
{
158.
con.Open();
159.
return
con;
160.
}
161.
catch
(Exception ex)
162.
{
163.
throw
ex;
164.
}
165.
}
166.
/// <summary>Oracle .NET DataProvider from Microsoft -- Standard Connection
167.
/// <para>ตัวอย่าง ConnectionString เช่น </para>
168.
/// <para>"Data Source=ServerName;User id=UserName;Password=Secret;"</para>
170.
/// </summary>
171.
public
static
System.Data.OracleClient.OracleConnection OracleConnection(
string
DataSource,
string
UserName,
string
Pwd =
""
)
172.
{
173.
return
OracleConnection(
"Data Source="
+DataSource+
";"
+
174.
"User id="
+UserName+
";"
+
175.
"Password="
+Pwd+
";"
);
176.
177.
}
178.
179.
#endregion
180.
}
181.
}