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
+ }
0 commit comments