> For the complete documentation index, see [llms.txt](https://kythuat.dtechvn.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kythuat.dtechvn.com/sql-server/sql-local-db.md).

# SQL LOCAL DB

## CÚ PHÁP LỆNH

#### 1. Kiểm tra thông tin sqllocaldb

* Vào run / cmd
* Tại cmd, gõ nội dung:&#x20;
  * ***sqllocaldb i***
  * màn hình sẽ liệt kê các intance đã cài đặt. Mặc định là DtechPro
* Nếu không nhìn thấy Dtechpro thì có thể là chưa cài đặt được sqllocaldb. Bạn cần cài đặt lại sqllocaldb bằng tay

#### 2. Kiểm tra, thay đổi trạng thái của sqllocaldb

* Kiểm tra trạng thái:
  * ***sqllocaldb i DtechPro***
  * Nhìn vào "**State"** để biết trạng thái của sql: Runing, stopped
* Câu lệnh Stop sql
  * ***sqllocaldb p DtechPro***
  * áp dụng khi muốn dừng sql để copy data
* Câu lệnh Start sql&#x20;
  * ***sqllocaldb s DtechPro***
  * Áp dụng khi muốn chạy sql lên do bị stop
* Muốn xoá intance dtechpro để cài lại intance mới
  * Bước 1: Dừng dịch vụ: ***sqllocaldb p DtechPro***
  * Bước 2: Xoá intance: ***sqllocaldb d DtechPro***
  * Bước 3: Nếu muốn tạo lại intance mới: ***sqllocaldb c DtechPro2***
  * ***Lưu ý:***&#x20;
    * Do tạo lại intance nên data sẽ không tồn tại trong intance mới
    * Cần attach lại data vào intance mới
    * Bước 1: Xoá hoặc sửa tên tệp Tram1.lih
    * Bước 2: Chạy exe
    * Bước 3: Bấm nút QLDulieu
    * Nhập thông tin kết nối:&#x20;
      * *(localdb)\dtechpro*
      * *Windows Authencation*
      * (với windows Authencation thì không cần user và mật khẩu)
      * Rồi Create hoặc Attach lại data cho intance mới

> ## SỬA LỖI SQLLOCALDB
>
> ### **1. sqllocaldb is not recognized as an internal or external command**

Lỗi này thường do thiếu đường dẫn tới file chạy sqlservr.exe trong System Variables của hệ thống windows, hãy COPY LINK DƯỚI ĐÂY.

```
C:\Program Files\Microsoft SQL Server\110\LocalDB\Binn
```

Hướng dẫn sửa lỗi:

* Click chuột phải vào biểu tượng My computer hoặc This PC ở ngoài màn hình Desktop, chọn Properties, (Nhấn vào Advanced System Settings đối với windows 11)

<figure><img src="/files/cGmHNxJT8a2BYx0p620M" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/rxLYocfNFjXWq8RsShT5" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/ApSNkzmK3LHOjRYwT2OD" alt=""><figcaption></figcaption></figure>

* Nhấn OK để đóng hết các cửa sổ đã mở lên.
* Mở lại cmd và thực hiện lệnh sqllocaldb i để kiểm tra.
* Cần Create lại instancename Dtechpro nếu cần thiết, start nó lên lại.

## NÂNG CAO VỀ SQLLOCALDB

1. Kiểm tra đường dẫn cài đặt SQLLOCALDB

* Bật CMD của windows lên, copy lệnh sau vào để chạy kiểm tra

```
reg query "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11E.LOCALDB\Setup" /v "SQLBinRoot"
```

<figure><img src="/files/mx4a88aZWpvzcx98HKU9" alt=""><figcaption></figcaption></figure>

(1) Là câu lệnh kiểm tra

(2) đường dẫn cài đặt được trả ra

* Lệnh DOS Kiểm tra tất cả nếu không biết chính xác

```
REM You wouldn't need the following SET if extracting with the FOR command
SET CheckPath="C:\Program Files\Microsoft SQL Server\110\LocalDB\Binn\sqlservr.exe"

IF EXIST %CheckPath% ( %CheckPath% -v ) ELSE ( ECHO Not really here )
```

2. Thực hiện truy vấn câu lệnh với sqlcmd

```
https://www.codeproject.com/Articles/1201271/Devs-User-Guide-To-SqlLocalDb-Sql-Express

https://www.mssqltips.com/sqlservertip/5612/getting-started-with-sql-server-2017-express-localdb/

https://www.mssqltips.com/sqlservertip/2694/getting-started-with-sql-server-2012-express-localdb/

https://dba.stackexchange.com/questions/152881/how-to-programmatically-find-out-where-localdb-is-installed
```
