ใช่ครับ ผมใช้แบบนั้นจริงๆ A เครื่องเดียวก็ลงฐานข้อมูลในเครื่อง A เลย
แต่ถ้าสมมุติ B กับ C เป็นบริษัทเดียวกัน อยู่คนละที่ อาจจะคนละจังหวัดต้องการใช้ฐานข้อมูลเดียวกัน
ก็ติดต่อผู้ให้บริการ internet ขอติดตั้ง Fix IP ก็สามารถชี้มาที่เดียวกัน ส่วนความเร็วนั้นอาจจะลดลงไป
แต่ถ้าคิดว่าไม่อยากไปนั่งลง SQL Server ไม่อยากไปนั่ง Restore เบส ก็ตาม No. 5 ครับ
ในตัวอย่างเป็นของ asp.net แต่หลักการใช้ linq ไม่ว่าจะเป็น web หรือ win เหมือนกัน
Intranet2012.designer.cs
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.261
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public partial class Intranet2012DataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public Intranet2012DataContext() :
base(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Intranet2012"].ConnectionString, mappingSource)
{
OnCreated();
}
public Intranet2012DataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public Intranet2012DataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public Intranet2012DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public Intranet2012DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
/*--------------------------------------------------------------*/
public bool TableExist(string TableName)
{
string checkCommand = string.Format("If Exists (Select * From sys.tables Where name = '{0}') Select 'true' Else Select 'false'", TableName);
bool result = Convert.ToBoolean(this.ExecuteQuery<string>(checkCommand).SingleOrDefault());
return result;
}
public Table<TableRole> RoleTable
{
get { return GetTable<TableRole>(); }
}
}
#pragma warning restore 1591
Intranet2012Datebase.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Web.Security;
using System.Data;
using System.Reflection;
using System.IO;
/// <summary>
/// Summary description for Intranet2012Datebase
/// </summary>
public class Intranet2012Datebase
{
public Intranet2012Datebase()
{
//
// TODO: Add constructor logic here
//
}
public static void PreparingDataBase()
{
Intranet2012DataContext intranet2012 = new Intranet2012DataContext();
if (!intranet2012.DatabaseExists())
{
intranet2012.CreateDatabase();
}
if (!intranet2012.TableExist("RoleTable"))
{
TableRole roleTable = new TableRole();
roleTable.Create();
}
FillInitailData(intranet2012);
}
protected static void FillInitailData(Intranet2012DataContext intranet2012)
{
if ((from r in intranet2012.RoleTable select r).Count() == 0)
{
var Rt = from rt in XElement.Load(HttpContext.Current.Request.MapPath("~/InitialData/Role.xml")).Elements("Role")
select rt;
// Execute the query
foreach (var rTable in Rt)
{
TableRole role = new TableRole();
if (rTable.Element("TypeID") != null) role.TypeID = Convert.ToInt16(rTable.Element("TypeID").Value);
if (rTable.Element("RoleName") != null) role.RoleName = rTable.Element("RoleName").Value;
intranet2012.RoleTable.InsertOnSubmit(role);
intranet2012.SubmitChanges();
if (!Roles.RoleExists(role.ID.ToString("00")))
Roles.CreateRole(role.ID.ToString("00"));
}
}
}
}
TableRole.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;
using System.Text;
/// <summary>
/// Summary description for UserType
/// </summary>
[Table(Name = "RoleTable")]
public class TableRole
{
private int id;
private int typeId;
private string roleName;
public TableRole()
{
//
// TODO: Add constructor logic here
//
}
[Column(Storage = "id", DbType = "Int not null Identity", IsPrimaryKey = true, CanBeNull = false, AutoSync = AutoSync.Default, IsDbGenerated = true)]
public int ID
{
get { return id; }
}
[Column(Storage = "typeId", DbType = "Int")]
public int TypeID
{
get { return typeId; }
set { typeId = value; }
}
[Column(Storage = "roleName", DbType = "NVarChar(64)")]
public string RoleName
{
get { return roleName; }
set { roleName = value; }
}
public void Create()
{
Intranet2012DataContext Intranet2012 = new Intranet2012DataContext();
StringBuilder SbCreate = new StringBuilder();
SbCreate.AppendLine("Create Table [RoleTable]");
SbCreate.AppendLine("(");
SbCreate.AppendLine("[ID] int Identity(1,1) Primary Key Clustered,");
SbCreate.AppendLine("[TypeID] int,");
SbCreate.AppendLine("[RoleName] nvarchar(64),");
SbCreate.AppendLine(")");
Intranet2012.ExecuteCommand(SbCreate.ToString());
}
}