希望能写一些简单的教程和案例分享给需要的人
欢迎进qq群交流:546496965
怎么使用 IDE 创建项目,可以参考我们的链接:使用开发工具开发一个打印输出程序 HelloWorld 你好世界~
https://daolizhe.blog.csdn.net/article/details/120095365
详细官方文档:https://gorm.io/zh_CN/docs/
安装依赖go get -u gorm.io/gorm
go get -u gorm.io/driver/sqlserver
代码:
package main
import (
"fmt"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)
type Student struct {
Id int
Name string
baibai string
}
func (Student) TableName() string {
return "Student"
}
func main() {
// 声明连接字符串
dsn := "sqlserver://cuyan:123456@120.79.19.125:1433?database=cuyan"
// 开启连接
db, _ := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
var students []Student
fmt.Println("gorm 执行 查询 student 表:")
// 查询表数据
db.Find(&students)
// 循环输出
for _, v := range students {
fmt.Println(v.Name)
}
}
结果
附表结构
表结构图
表结构代码
USE [cuyan]
GO
/****** Object: Table [dbo].[Student] Script Date: 2021/6/16 8:34:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Gender] [int] NULL,
[IsGraduate] [bit] NULL,
CONSTRAINT [PK_Student_1] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)