1
- package main
2
-
3
- import _ "github.com/denisenkom/go-mssqldb"
4
- import "database/sql"
5
- import "log"
6
- import "fmt"
7
-
8
- var server = "localhost"
9
- var port = 1433
10
- var user = "sa"
11
- var password = "your_password"
12
- var database = "SampleDB"
13
-
14
- // Create an employee
15
- func CreateEmployee (db * sql.DB , name string , location string ) (int64 , error ) {
16
- tsql := fmt .Sprintf ("INSERT INTO TestSchema.Employees (Name, Location) VALUES ('%s','%s');" ,
17
- name , location )
18
- result , err := db .Exec (tsql )
19
- if err != nil {
20
- fmt .Println ("Error inserting new row: " + err .Error ())
21
- return - 1 , err
22
- }
23
- return result .LastInsertId ()
24
- }
25
-
26
- // Read all employees
27
- func ReadEmployees (db * sql.DB ) (int , error ) {
28
- tsql := fmt .Sprintf ("SELECT Id, Name, Location FROM TestSchema.Employees;" )
29
- rows , err := db .Query (tsql )
30
- if err != nil {
31
- fmt .Println ("Error reading rows: " + err .Error ())
32
- return - 1 , err
33
- }
34
- defer rows .Close ()
35
- var count int = 0
36
- for rows .Next (){
37
- var name , location string
38
- var id int
39
- err := rows .Scan (& id , & name , & location )
40
- if err != nil {
41
- fmt .Println ("Error reading rows: " + err .Error ())
42
- return - 1 , err
43
- }
44
- fmt .Printf ("ID: %d, Name: %s, Location: %s\n " , id , name , location )
45
- count ++
46
- }
47
- return count , nil
48
- }
49
-
50
- // Update an employee's information
51
- func UpdateEmployee (db * sql.DB , name string , location string ) (int64 , error ) {
52
- tsql := fmt .Sprintf ("UPDATE TestSchema.Employees SET Location = '%s' WHERE Name= '%s'" ,
53
- location , name )
54
- result , err := db .Exec (tsql )
55
- if err != nil {
56
- fmt .Println ("Error updating row: " + err .Error ())
57
- return - 1 , err
58
- }
59
- return result .LastInsertId ()
60
- }
61
-
62
- // Delete an employee from database
63
- func DeleteEmployee (db * sql.DB , name string ) (int64 , error ) {
64
- tsql := fmt .Sprintf ("DELETE FROM TestSchema.Employees WHERE Name='%s';" , name )
65
- result , err := db .Exec (tsql )
66
- if err != nil {
67
- fmt .Println ("Error deleting row: " + err .Error ())
68
- return - 1 , err
69
- }
70
- return result .RowsAffected ()
71
- }
72
-
73
- func main () {
74
- // Connect to database
75
- connString := fmt .Sprintf ("server=%s;user id=%s;password=%s;port=%d;database=%s;" ,
76
- server , user , password , port , database )
77
- conn , err := sql .Open ("mssql" , connString )
78
- if err != nil {
79
- log .Fatal ("Open connection failed:" , err .Error ())
80
- }
81
- fmt .Printf ("Connected!\n " )
82
- defer conn .Close ()
83
-
84
- // Create employee
85
- createId , err := CreateEmployee (conn , "Jake" , "United States" )
86
- fmt .Printf ("Inserted ID: %d successfully.\n " , createId )
87
-
88
- // Read employees
89
- count , err := ReadEmployees (conn )
90
- fmt .Printf ("Read %d rows successfully.\n " , count )
91
-
92
- // Update from database
93
- updateId , err := UpdateEmployee (conn , "Jake" , "Poland" )
94
- fmt .Printf ("Updated row with ID: %d successfully.\n " , updateId )
95
-
96
- // Delete from database
97
- rows , err := DeleteEmployee (conn , "Jake" )
98
- fmt .Printf ("Deleted %d rows successfully.\n " , rows )
99
- }
1
+ package main
2
+
3
+ import (
4
+ "database/sql"
5
+ "fmt"
6
+ "log"
7
+
8
+ _ "github.com/denisenkom/go-mssqldb"
9
+ )
10
+
11
+ var (
12
+ server = "localhost"
13
+ port = 1433
14
+ user = "sa"
15
+ password = "your_password"
16
+ database = "SampleDB"
17
+ )
18
+
19
+ // CreateEmployee create an employee
20
+ func CreateEmployee (db * sql.DB , name string , location string ) (int64 , error ) {
21
+ tsql := fmt .Sprintf ("INSERT INTO TestSchema.Employees (Name, Location) VALUES ('%s','%s');" ,
22
+ name , location )
23
+ result , err := db .Exec (tsql )
24
+ if err != nil {
25
+ fmt .Println ("Error inserting new row: " + err .Error ())
26
+ return - 1 , err
27
+ }
28
+ return result .LastInsertId ()
29
+ }
30
+
31
+ // ReadEmployees read all employees
32
+ func ReadEmployees (db * sql.DB ) (int , error ) {
33
+ tsql := fmt .Sprintf ("SELECT Id, Name, Location FROM TestSchema.Employees;" )
34
+ rows , err := db .Query (tsql )
35
+ if err != nil {
36
+ fmt .Println ("Error reading rows: " + err .Error ())
37
+ return - 1 , err
38
+ }
39
+ defer rows .Close ()
40
+ count := 0
41
+ for rows .Next () {
42
+ var name , location string
43
+ var id int
44
+ err := rows .Scan (& id , & name , & location )
45
+ if err != nil {
46
+ fmt .Println ("Error reading rows: " + err .Error ())
47
+ return - 1 , err
48
+ }
49
+ fmt .Printf ("ID: %d, Name: %s, Location: %s\n " , id , name , location )
50
+ count ++
51
+ }
52
+ return count , nil
53
+ }
54
+
55
+ // UpdateEmployee update an employee's information
56
+ func UpdateEmployee (db * sql.DB , name string , location string ) (int64 , error ) {
57
+ tsql := fmt .Sprintf ("UPDATE TestSchema.Employees SET Location = '%s' WHERE Name= '%s'" ,
58
+ location , name )
59
+ result , err := db .Exec (tsql )
60
+ if err != nil {
61
+ fmt .Println ("Error updating row: " + err .Error ())
62
+ return - 1 , err
63
+ }
64
+ return result .LastInsertId ()
65
+ }
66
+
67
+ // DeleteEmployee delete an employee from database
68
+ func DeleteEmployee (db * sql.DB , name string ) (int64 , error ) {
69
+ tsql := fmt .Sprintf ("DELETE FROM TestSchema.Employees WHERE Name='%s';" , name )
70
+ result , err := db .Exec (tsql )
71
+ if err != nil {
72
+ fmt .Println ("Error deleting row: " + err .Error ())
73
+ return - 1 , err
74
+ }
75
+ return result .RowsAffected ()
76
+ }
77
+
78
+ func main () {
79
+ // Connect to database
80
+ connString := fmt .Sprintf ("server=%s;user id=%s;password=%s;port=%d;database=%s;" ,
81
+ server , user , password , port , database )
82
+ conn , err := sql .Open ("mssql" , connString )
83
+ if err != nil {
84
+ log .Fatal ("Open connection failed:" , err .Error ())
85
+ }
86
+ fmt .Printf ("Connected!\n " )
87
+ defer conn .Close ()
88
+
89
+ // Create employee
90
+ createID , err := CreateEmployee (conn , "Jake" , "United States" )
91
+ if err != nil {
92
+ log .Fatal ("CreateEmployee failed:" , err .Error ())
93
+ }
94
+ fmt .Printf ("Inserted ID: %d successfully.\n " , createID )
95
+
96
+ // Read employees
97
+ count , err := ReadEmployees (conn )
98
+ if err != nil {
99
+ log .Fatal ("ReadEmployees failed:" , err .Error ())
100
+ }
101
+ fmt .Printf ("Read %d rows successfully.\n " , count )
102
+
103
+ // Update from database
104
+ updateID , err := UpdateEmployee (conn , "Jake" , "Poland" )
105
+ if err != nil {
106
+ log .Fatal ("UpdateEmployee failed:" , err .Error ())
107
+ }
108
+ fmt .Printf ("Updated row with ID: %d successfully.\n " , updateID )
109
+
110
+ // Delete from database
111
+ rows , err := DeleteEmployee (conn , "Jake" )
112
+ if err != nil {
113
+ log .Fatal ("DeleteEmployee failed:" , err .Error ())
114
+ }
115
+ fmt .Printf ("Deleted %d rows successfully.\n " , rows )
116
+ }
0 commit comments