Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > CI เชื่อมต่อ SQL SERVER Express



 
Clound SSD Virtual Server

CI เชื่อมต่อ SQL SERVER Express

ติดตั้ง Server 2012 R2
Host จริงแนะนำ OS ให้เป็น Server (ในที่นี่ผมใช้ SERVER 2012 R2)
คลิก Next
ติดตั้ง Server 2012-Part1
เลือก Server with a GUI
ติดตั้ง Server 2012-Part2
เลือก Custom: Install Windows Only
ติดตั้ง Server 2012-Part3
New->Format คลิก Next
ติดตั้ง Server 2012-Part4
Installing...
ติดตั้ง Server 2012-Part5
ตั้งรหัสผ่าน
ติดตั้ง Server 2012-Part6
จบขั้นตอนติดตัั้ง SERVER 2012 R2 แบบ Basic


ติดตั้ง Microsoft® SQL Server® 2017 Express
ดาวน์โหลด
https://www.microsoft.com/en-us/download/details.aspx?id=55994
หลังจากเราดาวน์โหลดมาแล้ว ก็มาติดตั้งกันเลย
หมายเหตุ: ติดตั้งต้องใช้ Internet ด้วยนะครับ
sql-1
เลือก Basic (Config ภายหลังได้)
sql-2
คลิก Accept
sql-3
Installing...รอยาวๆไป
sql-4
Installation Completed. ให้ Restart 1 ที
sql-5


ติดตั้ง Microsoft SQL SERVER Management Studio
ดาวน์โหลดตาม Link
https://go.microsoft.com/fwlink/?linkid=873126
คลิก Install
ssms-1
Installing...รอยาวไป
ssms-2
Installation Complete. Restart อีกรอบ
ssms-3

SQL SERVER Configuration Manager
-เปิด SQL SERVER Configuration Manager ขึ้นมา
-Start Service: SQL SERVER (SQLEXPRESS), SQL SERVER BROWSER ตามรูป
config-1
คลิกขวาที่ SQL Server BROWSER \ Properties ไปที่ Tap: Service->Start Mode เลือก Automatic
config-2

ตั้งค่า Network Configuration: ที่หัวข้อ SQL Server Network Configuration
ให้ Enabled 3 Protocal ตามรูป
config-4
ตั้งค่า TCP/IP: คลิกขวา->Properties->ที่ Tap: IP Addresses
กำหนด TCP Port: 1433 ทุกหัวข้อ IP
ที่หัวข้อ IPALL
TCP Dynamic Ports: ว่าง
config-5
ตั้งค่า Firewall: Control Panel->Windows Firewall->Advance Settings->Inbound Rules
-New Rule->Port คลิก Next
-Specific Local Ports: 1433
-คลิก Next, Next, Next ตั้งค่า Name: Allow SQL Server
firewall


เริ่มใช้งาน SQL Server Management Studio (SSMS)
SQL Server Management Studio ขอเรียกสั้นๆว่า SSMS
-เปิดโปรแกรมขึ้นมา Authentication: เลือก Windows Authentication ไปก่อน
ssms-1
-ตั้งค่า SQL SERVER + Windows Authen:
-คลิกว่าที่ SQL SERVER->Properties
ssms-2
-ที่หัวข้อ Security: เลือก SQL Server and Windows Authentication mode ตามรูป
ssms-3


Add new User:
-ที่หัวข้อ Security: คลิกขวา->New Login
authen-1
-กรอก Login Name: admin
-ติ๊กเครื่องหมายถูกที่หน้า Enforce Password Policy ออก
authen-2
-กำหนด Server Rules, User Mapping
-status->login: Enabled
authen-3
-Restart Service 1 รอบ
authen-4

-จากนั้นทดสอบ Login จากเครื่องในวง LAN: SQL Server Mode
login
-ตั้ง SSMS เพิ่มเติม: เพื่อให้สามารถ Save แก้ไข Table ได้
-Tools->Options...
option-1
-Designer: Table and Database Designers
ติ๊กเครื่องหมายถูกที่หน้า: Prevent saving changes that require table re-creation ออก
option-2


ทดสอบ สร้าง Database:
-Create Database: คลิกขวา Database->New Database...
create-db
ตั้งชื่อ Database name: test
-new-db
ตั้งค่า Options->Collation: Thai_CI_AS
collation
-คลิก OK


ทดสอบ สร้าง Table:
-คลิกขวาที่ Tables->Table...
new-table
exam
-ทดสอบเพิ่มข้อมูล
Code (PHP)
INSERT ข้อมูล INSERT INTO (datail) VALUES ('xxxx');

insert


ฝั่ง Hosting:
-extension จะต้อง support: sqlsvr
extension
-หากไม่มี php_sqlsvr แจ้ง PM มานะครับ เด๋วส่ง e-mail ให้ 555


ฝั่ง CI Framework:
Config->Database:
Code (PHP)
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = "192.168.0.6";
$db['default']['port'] = 1433;
$db['default']['username'] = 'admin';
$db['default']['password'] = '123456';
$db['default']['database'] = 'test';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE; 
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;


เพื่อความสะดวก config: autoload->database เพิ่มอีกนิด
Code (PHP)
$autoload['libraries'] = array('database', 'session');


ทดสอบ Connect: ที่ Controller เขียนแบบง่ายละกัน ใช้คำสั่ง Active Record เหมือน MySQL เลยครับ
ตัว Test ผมเขียน Query ที่ Controller เลยละกัน (ปกติจะนิยมเขียนที่ Model)
Code (PHP)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$data['data'] = $this->db->query("SELECT * FROM exam")->result();
		$this->load->view('test_view', $data);
	}
}


View: ก็ทดสอบง่ายๆเช่นกัน อิอิ
Code (PHP)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test View</title>
</head>
<body>
<?php
foreach ($data as $row):
	echo $row->id,': ',$row->detail,'<br>';
endforeach;
?>
</body>
</html>


Result:
result

เพียงแค่นี้ก็สามารถเปลี่ยน Database จาก MySQL มาเป็น MSSQL ได้แล้วครับ


Reference : http://xman





   
Share
Bookmark.   

  By : xMan
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2018-05-19
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่