using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TORServices
{
public static class clsDatabase
{
public enum EnumConnectionType { ODBC, OLEDB, SqlClient }
public static string connectionString="";
public static System.Data.DataTable GetTableForm(string _SQL, EnumConnectionType ConnectionType)
{
System.Data.DataSet ds = new System.Data.DataSet();
switch (ConnectionType)
{
case EnumConnectionType.ODBC:
System.Data.Odbc.OdbcConnection conn1 = new System.Data.Odbc.OdbcConnection(connectionString);
conn1.Open();
System.Data.Odbc.OdbcDataAdapter adapter1 = new System.Data.Odbc.OdbcDataAdapter(_SQL, conn1);
adapter1.Fill(ds);
break;
case EnumConnectionType.OLEDB:
System.Data.OleDb.OleDbConnection conn2 = new System.Data.OleDb.OleDbConnection(connectionString);
conn2.Open();
System.Data.OleDb.OleDbDataAdapter adapter2 = new System.Data.OleDb.OleDbDataAdapter(_SQL, conn2);
adapter2.Fill(ds);
break;
case EnumConnectionType.SqlClient:
System.Data.SqlClient.SqlConnection conn3 = new System.Data.SqlClient.SqlConnection(connectionString);
conn3.Open();
System.Data.SqlClient.SqlDataAdapter adapter3 = new System.Data.SqlClient.SqlDataAdapter(_SQL, conn3);
adapter3.Fill(ds);
break;
}
return ds.Tables[0];
}
public static System.Windows.Forms.ComboBox DatabaseInCombobox(string _SQL, string strConn, EnumConnectionType ConnectionType)
{
ComboBox cmb = new ComboBox();
cmb.DataSource = GetTableForm(_SQL, ConnectionType);
return cmb;
}
public static string[] GetData(string field, string table, string strConn, EnumConnectionType ConnectionType)
{
DataTable dt = GetTableForm("Select " + field + " FROM " + table, ConnectionType);
string[] str = new string[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
str[i] = dt.Rows[i][field].ToString();
}
return str;
}
public static DataRow GetDataRow(string sql, string strConn, EnumConnectionType ConnectionType)
{
DataTable dt = GetTableForm(sql, ConnectionType);
return dt.Rows[0];
}
}
}
การใช้งานก็แค่เรียกใช้แบบบ้านโดยไม่ต้องประกาศ ไม่ต้องอยากรู้เรื่อง connection open หรือไม่ open 555
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TORServices.clsDatabase.connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=X:\\DATA\\PostCode.mdb;Jet OLEDB:Database Password=4410210091";
comboBox1.DataSource = TORServices.clsDatabase.GetTableForm("SELECT Amphur FROM Sett_Post_Amphur", TORServices.clsDatabase.EnumConnectionType.OLEDB);
comboBox1.DisplayMember = "Amphur";
dataGridView1.DataSource = TORServices.clsDatabase.GetTableForm("SELECT * FROM Sett_Post_PostCode", TORServices.clsDatabase.EnumConnectionType.OLEDB);
}
}
}
Public Class CascadingHandler
Implements IHttpHandler
#Region "IHttpHandler Members"
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim purpose As String = context.Request.QueryString("purpose") 'Province, Amphoe_IDC, Tambon_IDC
Dim sbJSON As New StringBuilder()
'Make json format [{"PROV_NAM_T":"หอยระทม","PROV_CODE":"1"},{"PROV_NAM_T":"หอยยิ้ม","PROV_CODE":"2"}]
'Dim strDQ As String = """" ' """" VB.NET Make String " \" C# Make String "
Dim iRowCount As Integer = 1
Select Case purpose
Case "P" 'Province
sbJSON.Append("[")
For i As Integer = 0 To iRowCount
sbJSON.Append("{")
sbJSON.Append(strDQ & "PROV_NAM_T" & strDQ & ":" & strDQ & "ตาก" & strDQ)
sbJSON.Append(",")
sbJSON.Append(strDQ & "PROV_CODE" & strDQ & ":" & strDQ & "ตาก" & strDQ)
sbJSON.Append("}" & If(i < iRowCount, ",", ""))
Next
sbJSON.Append("]")
Case "A" 'Amphoe
End Select
End Sub
#End Region
End Class