|
|
|
C# WinApp เอา connection - strings มาฝาก ครับ |
|
|
|
|
|
|
|
พอดีช่วง 2 -3 วันนั่งงมยุกับ DataGridView ค้นไปค้นมาเจอเว็บหนึ่งครับ
http://www.dofactory.com
เจอ connection-strings น่าสนใจเลยเอามาเป็น class เผื่อได้ใช้
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TORServices.Database
{
public static class Databaseconnection
{
/// <summary> ตรวจสอบ ข้อความว่าอยู่ในรูปแบบ IP หรือไม่
/// <para>ตัวอย่าง เช่น </para>
/// <para> "192.168.23.55".CheckIP()== True </para>
/// <para>อ้างอิงจาก TOR@ChemCoM </para>
/// </summary>
public static bool CheckIP(this string input)
{
System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"(^\d{1,}\.\d{1,}\.\d{1,}\.\d{1,}\z)");
return rex.IsMatch(input);
}
#region _ODBC
/// <summary>ODBC
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Dsn=DsnName;Uid=UserName;Pwd=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.Odbc.OdbcConnection OdbcConnection(string connectionString = "")
{
System.Data.Odbc.OdbcConnection con = new System.Data.Odbc.OdbcConnection(connectionString);
try{con.Open();return con;}catch (Exception ex){throw ex;}
}
/// <summary>ODBC DSN:Microsoft SQL Server,Oracle,MySql
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Dsn=DsnName;Uid=UserName;Pwd=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.Odbc.OdbcConnection OdbcConnection(string DsnName, string UserName = "", string Pwd = "")
{
return OdbcConnection("Dsn=" + DsnName + ";Uid="+UserName+";Pwd=" + Pwd + ";");
}
/// <summary>ODBC DSN:Microsoft Access
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\myPath\myDb.mdb;Uid=Admin;Pwd=;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.Odbc.OdbcConnection OdbcConnection(string Filename, string Pwd = "")
{
return OdbcConnection("Driver={Microsoft Access Driver (*.mdb)};Dbq="+Filename+";Uid=Admin;Pwd="+Pwd+";");
}
#endregion
#region _OleDb
/// <summary>OleDb
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Driver=SQLOLEDB;Data Source=ServerName;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.OleDb.OleDbConnection OleDbConnection(string connectionString = "")
{
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
try{con.Open();return con;}catch (Exception ex){throw ex;}
}
/// <summary>OleDb -- Standard Connection:Microsoft SQL Server
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Driver=SQLOLEDB;Data Source=ServerName;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.OleDb.OleDbConnection OleDbConnection(string DataSource,string DataBaseName,string UserName, string Pwd = "")
{
return OleDbConnection("Driver=SQLOLEDB;Data Source=" + DataSource + ";Initial Catalog=" + DataBaseName + ";User id=" + UserName + ";Password=" + Pwd + ";");
}
/// <summary>OleDb -- via IP Address:Microsoft SQL Server
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Driver=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.OleDb.OleDbConnection OleDbConnection( string DataSource,string IP , string DataBaseName, string UserName, string Pwd = "",string port = "1433")
{
return (!IP.CheckIP()) ? null : OleDbConnection("Driver=SQLOLEDB;Data Source=" + IP + "," + port + ";Initial Catalog=" + DataBaseName + ";User id=" + UserName + ";Password=" + Pwd + ";");
}
/// <summary>OleDb with MS Jet -- Standard Security
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mypath\myDb.mdb;User id=admin;Password="</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.OleDb.OleDbConnection OleDbConnection( string Filename, string Pwd = "")
{
return OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Filename+";User id=admin;"+ ";Password=" + Pwd + ";");
}
#endregion
#region _SqlClient
/// <summary>SqlClient
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Data Source=ServerName;Initial Catalog=DataBaseName;User id=UserName;Password=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.SqlClient.SqlConnection SqlConnection(string connectionString = "")
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
try
{
con.Open();
return con;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>.NET DataProvider -- Standard Connection:Microsoft SQL Server
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Data Source=ServerName;Initial Catalog=DataBaseName;Integrated Security=SSPI;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.SqlClient.SqlConnection SqlConnection(string DataSource, string DataBaseName, string UserName, string Pwd = "")
{
return SqlConnection("Data Source=" + DataSource + ";" + "Initial Catalog=" + DataBaseName + ";" +"User id=" + UserName + ";" +"Password=" + Pwd + ";");
}
/// <summary>.NET DataProvider -- via IP Address:Microsoft SQL Server
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=DataBaseName;User Id=UserName;Password=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.SqlClient.SqlConnection SqlConnection( string IP, string DataBaseName, string UserName, string Pwd = "", string port = "1433")
{
return (!IP.CheckIP())?null: SqlConnection("Network Library=DBMSSOCN;Data Source="+IP+","+port+";Initial Catalog="+DataBaseName+";User Id="+UserName+";Password="+Pwd+";");
}
/// <summary>Microsoft Sql Express.NET Data Provider -- Default Relative Path -- Standard Connection:Microsoft SQL Server
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Data Source=.\SQLExpress;User Instance=true;User Id=UserName;Password=Secret;AttachDbFilename=|DataDirectory|DataBaseName.mdf;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.SqlClient.SqlConnection SqlConnection(string DataBaseName, string UserName, string Pwd = "" )
{
return SqlConnection("Data Source=.\\SQLExpress;User Instance=true;User Id="+UserName+";Password="+ Pwd+";AttachDbFilename="+DataBaseName+";");
}
#endregion
#region _OracleClient
/// <summary>OracleClient
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Data Source=ServerName;User id=UserName;Password=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.OracleClient.OracleConnection OracleConnection(string connectionString = "")
{
System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(connectionString);
try
{
con.Open();
return con;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>Oracle .NET DataProvider from Microsoft -- Standard Connection
/// <para>ตัวอย่าง ConnectionString เช่น </para>
/// <para>"Data Source=ServerName;User id=UserName;Password=Secret;"</para>
/// <para>อ้างอิงจาก http://www.dofactory.com/reference/connection-strings </para>
/// </summary>
public static System.Data.OracleClient.OracleConnection OracleConnection(string DataSource, string UserName, string Pwd = "")
{
return OracleConnection("Data Source="+DataSource+";" +
"User id="+UserName+";" +
"Password="+Pwd+";");
}
#endregion
}
}
Tag : .NET, Win (Windows App), C#, VS 2012 (.NET 4.x)
|
|
|
|
|
|
Date :
2016-09-26 23:02:46 |
By :
lamaka.tor |
View :
1201 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จัดไปครับ
|
|
|
|
|
Date :
2016-09-27 16:28:37 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|