Read CF
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;
namespace TrafficPolice.Class
{
class MobileDataAccess
{
#region "Member"
private static string path = Assembly.GetExecutingAssembly().GetName().CodeBase;
private static string file = Path.Combine(Path.GetDirectoryName(path), @"DataBase\GiffyHackman.sdf");
private static SqlCeConnection conn = new SqlCeConnection(string.Format("Data Source={0};Password=;", file));
private static SqlCeDataAdapter adp2 = new SqlCeDataAdapter();
private static SqlCeCommand com2 = new SqlCeCommand();
ConnectWS ConWs = new ConnectWS();
#endregion
public DataSet Excution(ref DataSet ds, string sqlStr, string tbName, ref string Errmsg)
{
if (MobileConfiguration.Settings["UseWebService"].ToString() == "Y")
{
return ConWs.Excution(ref ds, sqlStr, tbName, ref Errmsg);
}
else
{
try
{ //SQL Moblie
com2 = new SqlCeCommand(sqlStr, conn);
conn.Open();
adp2.SelectCommand = com2;
adp2.Fill(ds, tbName);
return ds;
}
catch (System.Exception ex)
{
Errmsg += ex.Message.ToString();
conn.Close();
return ds;
}
finally
{
//SQL Moblie
conn.Close();
}
}
}
public bool ExcutionCmd(string sqlStr, ref string ErrMsg)
{
if (MobileConfiguration.Settings["UseWebService"].ToString() == "Y")
{
return ConWs.ExcutionCmd(sqlStr, ref ErrMsg);
}
else
{
try
{//SQL Moblie
com2.Connection = conn;
conn.Open();
com2.CommandText = sqlStr;
com2.ExecuteNonQuery();
conn.Close();
return true;
}
catch (System.Exception ex)
{
conn.Close();
throw ex;
}
finally
{
//SQL Moblie
conn.Close();
}
}
}
}
}
Ex2.Read Config
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Collections.Specialized;
namespace TrafficPolice.Class
{
public class MobileConfiguration
{
public static NameValueCollection Settings;
static MobileConfiguration()
{
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
string configFile = Path.Combine(appPath, @"App.config");
if (!File.Exists(configFile))
{
throw new FileNotFoundException(string.Format("Application configuration file '{0}' not found.", configFile));
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(configFile);
XmlNodeList nodeList = xmlDocument.GetElementsByTagName("appSettings");
Settings = new NameValueCollection();
foreach (XmlNode node in nodeList)
{
foreach (XmlNode key in node.ChildNodes)
{
Settings.Add(key.Attributes["key"].Value, key.Attributes["value"].Value);
}
}
}
}
}
Config file Ex
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ServerName" value="GIFFYServer" />
<add key="ServerPort" value="1433" />
<add key="UserName" value="sa" />
<add key="Password" value="Pass" />
<add key="DataBaseName" value ="GiffyDB" />
<add key="UrlWebServices" value ="http://xxxxxx/Service1.asmx"/>
<add key="UseWebService" value ="Y"></add>
</appSettings>
</configuration>
Connect SQL Server
using System.Data;
using System.Reflection;
using System.Text;
using System.Data.SqlServerCe;
using System.Data.SqlClient;
using System.IO;
using System;
using System.Net;
using System.Net.Sockets;
namespace PDASaveCost.Class
{
class DataAccess
{
private static SqlConnection con = new SqlConnection();
private static SqlDataAdapter adp = new SqlDataAdapter();
private static SqlCommand com = new SqlCommand();
private static string IPAddressX;
public static DataSet Excution(ref DataSet ds, string sqlStr, string tbName, ref string Errmsg)
{
try
{
con.ConnectionString = getConnectionString();
con.Open();
com = new SqlCommand(sqlStr, con);
adp.SelectCommand = com;
adp.Fill(ds, tbName);
return ds;
}
catch (Exception ex)
{
Errmsg += ex.Message;
con.Close();
return ds;
}
finally
{
con.Close();
}
}
public static DataSet Excution(ref DataSet ds, string sqlStr, ref string Errmsg)
{
try
{
con.ConnectionString = getConnectionString();
con.Open();
com = new SqlCommand(sqlStr, con);
adp.SelectCommand = com;
adp.Fill(ds);
return ds;
}
catch (System.Exception ex)
{
Errmsg += ex.Message;
con.Close();
return ds;
}
finally
{
con.Close();
}
}
public static DataTable Excution(ref DataTable dt, string sqlStr, ref string Errmsg)
{
try
{
//SQL Server
con.ConnectionString = getConnectionString();
con.Open();
com = new SqlCommand(sqlStr, con);
adp.SelectCommand = com;
adp.Fill(dt);
return dt;
}
catch (System.Exception ex)
{
Errmsg += ex.Message;
con.Close();
return dt;
}
finally
{
//SQL Server
con.Close();
}
}
//Get Configuration files in Text config.txt
private static string getConnectionString()
{
string conStr = null;
string Server = MobileConfiguration.Settings["ServerName"];
string port = MobileConfiguration.Settings["ServerPort"];
string uid = MobileConfiguration.Settings["UserName"];
string pwd = MobileConfiguration.Settings["Password"];
string database = MobileConfiguration.Settings["DataBaseName"];
if (GetIPAddPEER() != "")
{
Server = IPAddressX + ", " + port + "";
}
conStr = " Server= " + Server + ";";
conStr += "uid= " + uid + " ;";
conStr += "pwd=" + pwd + ";";
conStr += "database=" + database + "";
return conStr;
}
//Get IPAddress By : GiffyHackman
public static string GetIPAddPEER()
{
try
{
IPAddress[ hostAddr = Dns.GetHostEntry("PPP_PEER").AddressList;
for (int i = 0; i < hostAddr.Length; i++) // Loop find IP
{
IPAddressX = hostAddr
.ToString();
}
}
catch (Exception)
{
IPAddressX = ""; //NULL Return ""
}
return IPAddressX; //Return IP
}
}
}
ปล. Code นี้อาจจะดูถึกๆ ไปหน่อยนะคับบ (แต่ละคนก็มี Style การเขียนที่แตกต่างกันไป บางท่านอาจเขียนได้กระชับกว่านี้ก็ได้ แล้วแต่ประสบการณของแต่ล่ะท่าน ... แต่ผมประสบการณ์ยังน้อยย เลยเขียนออกมาถืกๆ แบบนี้อะคับ เหอๆๆ ลอง adap ใช้เอานะคับ หรือดูจากเว็บผมก็ได้คับ www.giffyhackman.com/webboard)