将SQL Server连接到Golang的gorm

huangapple go评论91阅读模式
英文:

Connecting SQL Server to Golang gorm

问题

我是新手,正在尝试将gorm连接到远程SQL Server数据库时感到困惑。

在.NET中,我通常在web.config中使用以下代码:

<add name="DevSaveLog" connectionString="Data Source=11.111.1.111;Network Library=DBMSSOCN;Initial Catalog=Database_Log;User ID=user_log;Password=dhhdf127ihd" providerName="System.Data.SqlClient" />

当我尝试连接gorm时,我尝试了以下代码:

func Init() {
    dsn := url.QueryEscape("sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log")
    db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
    if err != nil {
        panic("error connecting to database")
    }
    db.AutoMigrate()
}

但是我遇到了错误:

[error] failed to initialize database, got error unable to open tcp connection with host 'localhost:1433': dial tcp 127.0.0.1:1433: connectex: No connection could be made because the target machine actively refused it.
panic: error connecting to database

顺便说一下,我在这个问题中没有使用真实的URL。即使我已经声明了sqlserver数据库的URL服务器,它仍然在读取localhost。

有什么解决办法吗?

谢谢 将SQL Server连接到Golang的gorm

英文:

I am new in Golang btw. I got confused when I try to connect gorm to remote SQL Server database.

In .NET I usually use this for web.config

&lt;add name=&quot;DevSaveLog&quot; connectionString=&quot;Data Source=11.111.1.111;Network Library=DBMSSOCN;Initial Catalog=Database_Log;User ID=user_log;Password=dhhdf127ihd&quot; providerName=&quot;System.Data.SqlClient&quot; /&gt;

And when I try to connect to gorm I tried like this

func Init() {
dsn := url.QueryEscape(&quot;sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log&quot;)
db, err := gorm.Open(sqlserver.Open(dsn), &amp;gorm.Config{})
if err != nil {
	panic(&quot;error connecting to database&quot;)
}
db.AutoMigrate()

}

And I got errors :

[error] failed to initialize database, got error unable to open tcp connection with host &#39;localhost:1433&#39;: dial tcp 127.0.0.1:1433: connectex: No connection could be made because the target machine actively refused it.

panic: error connecting to database

BTW I don't use the real URL in this question .Even I have declared the url server of sqlserver database it just keep reading localhost.

Any solutions?

Thanks 将SQL Server连接到Golang的gorm

答案1

得分: 1

我可以看到一些问题(文档是一个很好的起点):

DSN字符串

sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log 不是一个有效的DSN(删除 http://)。

URL编码

url.QueryEscape("sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log") 返回 sqlserver%3A%2F%2Fuser_log%3Adhhdf127ihd%40http%3A%2F%2F11.111.1.111%3Fdatabase%3DDatabase_Log

这不是一个有效的URL,github.com/denisenkom/go-mssqldb 不会正确处理它,因为它会查找 sqlserver:// 前缀(参考 这里)。删除 url.QueryEscape

总结一下,尝试像这样的代码:

dsn := "sqlserver://user_log:dhhdf127ihd@11.111.1.111?database=Database_Log"
db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
英文:

I can see a few issues (the docs are a good place to start):

DSN String

sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log is not a valid DSN (remove the http://).

URL Encoding

url.QueryEscape(&quot;sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log&quot;) returns sqlserver%3A%2F%2Fuser_log%3Adhhdf127ihd%40http%3A%2F%2F11.111.1.111%3Fdatabase%3DDatabase_Log.

This is not a valid URL and will not be correctly processed by github.com/denisenkom/go-mssqldb which looks for a prefix of sqlserver://. Remove the url.QueryEscape.

In summary try something like:

dsn := &quot;sqlserver://user_log:dhhdf127ihd@11.111.1.111?database=Database_Log&quot;
db, err := gorm.Open(sqlserver.Open(dsn), &amp;gorm.Config{})

huangapple
  • 本文由 发表于 2022年7月14日 17:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/72978126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定