Created
September 27, 2019 05:39
-
-
Save hymkor/f28e049aa913ea46a8073250a8be0a0f to your computer and use it in GitHub Desktop.
SQL Server へ接続してみるテスト(業務ワードは「...」に書き換えたので、修正が必要です)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"database/sql" | |
"fmt" | |
_ "github.com/denisenkom/go-mssqldb" | |
) | |
const ( | |
CONNECT_STRING = `server=....;user id=sa;password=....;database=....` | |
) | |
func main1() error { | |
conn, err := sql.Open("sqlserver", CONNECT_STRING) | |
if err != nil { | |
return err | |
} | |
ctx := context.Background() | |
if err := conn.PingContext(ctx) ; err != nil { | |
return err | |
} | |
rows,err := conn.QueryContext(ctx,`SELECT ....,.... FROM ....;`) | |
if err != nil { | |
return fmt.Errorf("QueryContext: %v",err) | |
} | |
defer rows.Close() | |
for rows.Next() { | |
var serial int | |
var name string | |
err := rows.Scan(&serial,&name) | |
if err != nil { | |
return err | |
} | |
fmt.Printf("....=%d ....=%s\n",serial,name) | |
} | |
return nil | |
} | |
func main(){ | |
if err := main1() ; err != nil { | |
println(err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment