denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.82k stars 497 forks source link

sqlserver password contain "@" can't connect the datebase #651

Closed xiaogu-space closed 3 years ago

xiaogu-space commented 3 years ago

Describe the bug A clear and concise description of what the bug is.

If you are seeing an exception, include the full exceptions details (message and stack trace).

In my case, the password having @, then it will promote an error like this:
failed to initialize database, got error Login error: read tcp 10.0.1.12:34916->10.0.1.6:1555: read: connection reset by peer
panic: Login error: read tcp 10.0.1.12:34916->10.0.1.6:1555: read: connection reset by peer
xhit commented 3 years ago

This dsn should works if you try to connect with any password and host/instance:

dsn = fmt.Sprintf("sqlserver://%s:%s@%s?database=%s", cfg.Username, strings.ReplaceAll(url.QueryEscape(cfg.Password), "+", "%20"), cfg.Host, cfg.Database)

Without instance, only host and require port:

dsn = fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", cfg.Username, strings.ReplaceAll(url.QueryEscape(cfg.Password), "+", "%20"), cfg.Host, cfg.Port, cfg.Database)
xiaogu-space commented 3 years ago

thanks