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