blog.dotmaui.com Open in urlscan Pro
135.125.102.94  Public Scan

URL: https://blog.dotmaui.com/2021/01/15/connect-to-a-sql-server-database-with-go/
Submission: On December 07 via manual from GB — Scanned from FR

Form analysis 2 forms found in the DOM

GET https://blog.dotmaui.com/

<form role="search" method="get" class="search-form" action="https://blog.dotmaui.com/">
  <label>
    <span class="screen-reader-text">Search for:</span>
    <input type="search" class="search-field" placeholder="Search …" value="" name="s">
  </label>
  <input type="submit" class="search-submit" value="Search">
</form>

POST https://blog.dotmaui.com/wp-comments-post.php

<form action="https://blog.dotmaui.com/wp-comments-post.php" method="post" id="commentform" class="comment-form" novalidate="">
  <p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> <span class="required-field-message" aria-hidden="true">Required fields are marked <span class="required" aria-hidden="true">*</span></span></p>
  <p class="comment-form-comment"><label for="comment">Comment <span class="required" aria-hidden="true">*</span></label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required=""></textarea></p>
  <p class="comment-form-author"><label for="author">Name <span class="required" aria-hidden="true">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" required=""></p>
  <p class="comment-form-email"><label for="email">Email <span class="required" aria-hidden="true">*</span></label> <input id="email" name="email" type="email" value="" size="30" maxlength="100" aria-describedby="email-notes" required=""></p>
  <p class="comment-form-url"><label for="url">Website</label> <input id="url" name="url" type="url" value="" size="30" maxlength="200"></p>
  <p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment"> <input type="hidden" name="comment_post_ID" value="789" id="comment_post_ID">
    <input type="hidden" name="comment_parent" id="comment_parent" value="0">
  </p>
  <p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="e1967f4945"></p>
  <p style="display: none !important;"><label>Δ<textarea name="ak_hp_textarea" cols="45" rows="8" maxlength="100"></textarea></label><input type="hidden" id="ak_js_1" name="ak_js" value="1670378729952">
    <script>
      document.getElementById("ak_js_1").setAttribute("value", (new Date()).getTime());
    </script>
  </p>
</form>

Text Content

Skip to content
.Maui Blog
Examples and tutorials for programmers without advertising.
Menu
 * Home
 * .Maui APIs

Search for:
January 15, 2021


CONNECT TO A SQL SERVER DATABASE WITH GO

In this example, let’s see how to connect to Sql Server, select and view the
database version using Go programming language.

package main

import (
    _ "github.com/denisenkom/go-mssqldb"
    "database/sql"
    "context"
    "log"
    "fmt"
)

// Replace with your own connection parameters
var server = "MY_SERVER"
var port = 1433
var user = "MY_USERNAME"
var password_mssql = "MY_PASSWORD"

var db *sql.DB

func main() {
    var err error

    // Connection string
    connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d",
        server, user, password_mssql, port)

    // Connection pool
    db, err = sql.Open("sqlserver", connString)
    if err != nil {
        log.Fatal("Error creating connection pool: " + err.Error())
    }
    log.Printf("Connected!\n")
    
    // Close connection pool
    defer db.Close()

    ctx := context.Background() 
    err = db.PingContext(ctx)
    if err != nil {
        log.Fatal("Error pinging database: " + err.Error())
    }

    var result string

    // Run query and scan for result
    err = db.QueryRowContext(ctx, "SELECT @@version").Scan(&result)
    if err != nil {
        log.Fatal("Scan failed:", err.Error())
    }
    fmt.Printf("%s\n", result)
}



Golang
.Maui



POST NAVIGATION

Older post
Resize an image with Golang and dotmaui.com APIs
Newer post
Generate random numbers in GoLang


LEAVE A COMMENT CANCEL REPLY

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website





Δ

Powered by WordPress | Theme: Write by Themegraphy