结果图
涉及到的技术
1、HttpClient请求网络
2、webapi接口用法
3、sqlsugar数据库使用
4、Json序列化、反序列化
设计流程
刚开始做这个小项目,很懵。按照平常中项目的写法,就是先配置好数据库,服务层,控制层,再到前端调用,但是实际操作还是很多问题,下面是做完这个小项目学习总结的一个流程
一、后端(主要是操作数据库来为前端提供前端请求的数据)
1、在类库中创建公共DTO类(MODEL),SERVER层中也需要创建Model,但是为什么建设两个,因为,在SERVER层中的Model前端访问不了,其次,SERVER层需要操作数据库,会给Model层加一些特性标记,仅限于SERVER层去使用,如果只单独建一个公共类库中DTO(MODEL),那么,公共类库中需要引用SQLSUGAR包,不合规范
2、配置数据库DbContext
3、service层编写业务代码,注入数据库依赖DB
4、控制层添加接口特性、注入Iservice依赖service
5、startup文件中服务注入,凡是构造器中注入的服务都需要注入到startup文件中。注入服务的方式有三种:
builder.Services.AddSingleton();
builder.Services.AddScoped();
builder.Services.AddTransient();
6、调试接口
二、前端
1、配置httpclient服务,目的是获取网络(指定接口地址)的数据,注意凡是涉及到传输数据格式不为application/text格式,都需要在请求的头文件中修改请求头类型,content-type
2、设计前端页面,调用httpclient服务去虎丘数据
三、注意
1、Oracle数据库连接字符串
2、实体类字段类型要和数据库表字段类型保持一致
3、前端Httpclient请求的地址和调用接口返回的地址一致
4、前端解析返回的结果类型必须和后端返回的类型一致
5、每次List在循环中给T对象赋值时,需要将new T放在循环中,原理和创建行一致
6、当使用sqlsugar去修改数据时,主键数据不能被修改,只能修改非主键数据。
7、sqlsugar主键自增属性OracleSequenceName=“sequence表”,sequence表必须是默认创建的额,否则会冲突
代码展示
前端请求接口代码
public class WebClient
{
public HttpClient client;
public string baseurl;
public string token;
public WebClient()
{
client=new HttpClient();
client.Timeout =new TimeSpan(0,0,5,0);
baseurl = "https://localhost:7212/EMPInfo";
client.BaseAddress= new Uri(baseurl);
}
/// <summary>
/// 验证登录
/// </summary>
/// <param name="Userid"></param>
/// <param name="Pwd"></param>
/// <returns></returns>
public void LoginVerify(string Userid,string Pwd)
{
}
/// <summary>
/// get请求
/// </summary>
public string GetInfo()
{
using (client = new HttpClient())
{
HttpResponseMessage httpResponseMessage=new HttpResponseMessage();
//获取请求结果
httpResponseMessage = client.GetAsync(baseurl+ "/GetEmpInfo").Result;
if (httpResponseMessage != null)
{
//将请求的结果给到
return httpResponseMessage.Content.ReadAsStringAsync().Result;
}
return null;
}
}
/// <summary>
/// post保存
/// </summary>
public string SaveData(List<K_emp> config)
{
using (client=new HttpClient())
{
HttpResponseMessage responseMessage = new HttpResponseMessage();
var json = JsonConvert.SerializeObject(config);
//发送或解析JSON数据需要加头文件类型描述
HttpContent content = new StringContent(json,Encoding.UTF8);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
responseMessage = client.PostAsync(baseurl+ "/UpdateEmpInfo", content).Result;
return responseMessage.Content.ReadAsStringAsync().Result;
}
}
}
///删除
private void button4_Click(object sender, EventArgs e)
{
//选中哪一行,删除
DialogResult result = MessageBox.Show("是否真的要删除?");
if (result == DialogResult.OK)
{
WebClient wc = new WebClient();
int num = dataGridView1.SelectedRows.Count;
if (dataGridView1.CurrentRow.DataBoundItem is K_emp dto)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
{
//K_emp k_Emp = new K_emp();
//dto.EMPNO = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value.ToString());
var message= wc.DeleteEmpInfo(dto);
}
}
}
}
}
后端配置DBCONTEXT
public DbContext()
{
Db = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = "Data Source=(DESCRIPTION=" + "(ADDRESS=(PROTOCOL=TCP)(HOST=IP)(PORT=1521))" + "(CONNECT_DATA= (SERVER = DEDICATED)(SERVICE_NAME=ORCL)));" + "User Id=账号;Password=密码;",
DbType = DbType.Oracle,
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
IsAutoCloseConnection = true//开启自动释放模式和EF原理一样
});
//sqlsugar创建实体类
//client.CodeFirst.InitTables(typeof(K_emp));
//client.Aop.OnLogExecuting(SQL,Parameter)=>{
//};
}
其余的就是业务代码,没啥区别
总结
本次还用到了一个新鲜的写法
if (dataGridView1.DataSource is List<K_emp> config)
{
//这样就可以直接拿到K_EMP对象属性,直接操作
config.Capacity = "";
}
最后返回K_emp对象Config。
本次还是收获蛮大,很开心。