[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
首先新建数据库表
PRIMARY KEY (JY_ID)-表示唯一主键
--创建数据库的一张表 CREATE TABLE MYBAIDUJYLIST( JY_ID VARCHAR(200) NOT NULL,--唯一标识
JY_BH VARCHAR(200) NOT NULL,--编号 JY_MC VARCHAR(200) NOT NULL,--名称 JY_ViewCount
int,--浏览量 JY_DZCount int,--点赞数 JY_TPCount int,--投票数 JY_IFXS VARCHAR(10),--是否选上
JY_FBTime varchar(100),--发布时间 -- yyyymmdd PRIMARY KEY (JY_ID)--主键字段 唯一 )
select 可以查询数据表的内容,而insert则是插入数据库结果`
SELECT * FROM MYBAIDUJYLIST--查询 INSERT INTO MYBAIDUJYLIST
(JY_ID,JY_BH,JY_MC,JY_ViewCount,JY_DZCount,JY_TPCount,JY_IFXS,JY_FBTime)
VALUES(NEWID(),'fb00001','测试数据',100,2,13,1,'20191112')--插入数据
而C#中可以通过简单的配置连接数据库
比如下
通过简单配置当前数据库的相关信息,如下为sql server数据的相关信息
Data Source=服务器名称;Initial Catalog=数据库名称;User ID = 用户名;Pwd = 密码
如下代码可以查询数据库表的相关数据表数据,和插入一条数据,其中插入数据必须存在 sqlc.ExecuteNonQuery();
//connString = "Data Source=服务器名称;Initial Catalog=数据库名称;User ID = 用户名;Pwd =
密码"; //需要分别连接上服务器名称 数据库名称 用户名 密码才可以正确连接 conn.ConnectionString = connString;
conn.Open(); // 打开数据库连接 //查询sql String sql = " select * from MYBAIDUJYLIST"; //
查询语句 SqlDataAdapter myda = new SqlDataAdapter(sql, conn); // 实例化适配器 DataTable
dt = new DataTable(); // 实例化数据表 myda.Fill(dt); //插入sql sql = "INSERT INTO
MYBAIDUJYLIST
(JY_ID,JY_BH,JY_MC,JY_ViewCount,JY_DZCount,JY_TPCount,JY_IFXS,JY_FBTime)VALUES(NEWID(),'fb00001','测试数据',100,2,13,1,'20191112')";
//;" select * from MYBAIDUJYLIST"; // 查询语句 SqlCommand sqlc = new
SqlCommand(sql, conn); sqlc.ExecuteNonQuery(); conn.Close(); // 关闭数据库连接