public bool AddInstitute(InstituteInfo instituteInfo) { if ((instituteInfo.major == null) || (instituteInfo.major.Length == 0)) { MessageBox.Show("专业为空!"); return(false); } SQLiteConnection conn = new SQLiteConnection(dbPath);/* 创建数据库实例,指定文件位置 */ SQLiteCommand cmdQ = new SQLiteCommand(); try { conn.Open(); /* 打开数据库,若文件不存在会自动创建 */ SQLiteTransaction tran = conn.BeginTransaction(); cmdQ = new SQLiteCommand(conn); /* 实例化SQL命令 */ cmdQ.Transaction = tran; cmdQ.CommandText = "insert into institute values(@num, @name)"; /* 设置带参SQL语句 */ cmdQ.Parameters.AddRange(new[] { /* 添加参数 */ new SQLiteParameter("@num", instituteInfo.num), new SQLiteParameter("@name", instituteInfo.name) }); cmdQ.ExecuteNonQuery(); /* 执行查询 */ tran.Commit(); /* 提交 */ cmdQ.Dispose(); /* 释放资源 */ tran.Dispose(); /* 释放资源 */ tran.Dispose(); /* 释放资源 */ string sql = "CREATE TABLE IF NOT EXISTS " + instituteInfo.name + "(" + /* 建表语句 */ "name VARCHAR(30));"; /* 学院名称 */ cmdQ = new SQLiteCommand(sql, conn); cmdQ.ExecuteNonQuery(); /* 如果表不存在,创建单元表 */ for (int idx = 0; idx < instituteInfo.major.Length; idx++) { tran = conn.BeginTransaction(); cmdQ = new SQLiteCommand(conn); /* 实例化SQL命令 */ cmdQ.Transaction = tran; cmdQ.CommandText = "insert into " + instituteInfo.name + " values(@name)"; /* 设置带参SQL语句 */ cmdQ.Parameters.AddRange(new[] { /* 添加参数 */ new SQLiteParameter("@name", instituteInfo.major[idx]) }); cmdQ.ExecuteNonQuery(); /* 执行查询 */ tran.Commit(); /* 提交 */ cmdQ.Dispose(); /* 释放资源 */ tran.Dispose(); /* 释放资源 */ tran.Dispose(); /* 释放资源 */ } InitInstituteList(); /* 更新列表 */ } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); cmdQ.Dispose(); /* 释放资源 */ conn.Close(); return(false); } cmdQ.Dispose(); /* 释放资源 */ conn.Close(); return(true); }
public async Task <ActionResult <InstituteInfo> > PostInstituteInfo(InstituteInfo instituteInfo) { _context.InstituteInfo.Add(instituteInfo); await _context.SaveChangesAsync(); return(CreatedAtAction("GetInstituteInfo", new { id = instituteInfo.InstituteInfoId }, instituteInfo)); }
public async Task <IActionResult> PutInstituteInfo(int id, InstituteInfo instituteInfo) { if (id != instituteInfo.InstituteInfoId) { return(BadRequest()); } _context.Entry(instituteInfo).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InstituteInfoExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task UpdateInstitute(InstituteInfo instituteInfo, Guid instituteGuid) { var institute = await _dbContext.Institutes.FirstOrDefaultAsync(e => e.Id == instituteGuid); institute.Name = instituteInfo.Name; institute.SpecialtiesGuids = instituteInfo.SpecialtiesGuids; institute.Address = instituteInfo.Address; institute.Director = instituteInfo.Director; institute.Url = instituteInfo.Url; institute.Phone = instituteInfo.Phone; institute.Type = instituteInfo.Type; await _dbContext.SaveChangesAsync(); }
public async Task <Guid> CreateInstitute(InstituteInfo instituteInfo) { var institute = new Institute() { Id = Guid.NewGuid(), Name = instituteInfo.Name, SpecialtiesGuids = instituteInfo.SpecialtiesGuids, Address = instituteInfo.Address, Director = instituteInfo.Director, Url = instituteInfo.Url, Phone = instituteInfo.Phone, Type = instituteInfo.Type }; await _dbContext.Institutes.AddAsync(institute); await _dbContext.SaveChangesAsync(); return(institute.Id); }
public bool DelInstitute(InstituteInfo instituteInfo) { SQLiteConnection conn = new SQLiteConnection(dbPath);/* 创建数据库实例,指定文件位置 */ SQLiteCommand cmdQ = new SQLiteCommand(); try { conn.Open(); /* 打开数据库,若文件不存在会自动创建 */ SQLiteTransaction tran = conn.BeginTransaction(); cmdQ = new SQLiteCommand(conn); /* 实例化SQL命令 */ cmdQ.Transaction = tran; cmdQ.CommandText = "DELETE FROM institute where num = @num and name = @name"; /* 设置带参SQL语句 */ cmdQ.Parameters.AddRange(new[] { /* 添加参数 */ new SQLiteParameter("@num", instituteInfo.num), new SQLiteParameter("@name", instituteInfo.name) }); cmdQ.ExecuteNonQuery(); /* 执行查询 */ tran.Commit(); /* 提交 */ tran.Dispose(); /* 释放资源 */ string sql = "DROP TABLE " + instituteInfo.name; /* 建表语句 */ cmdQ = new SQLiteCommand(sql, conn); cmdQ.ExecuteNonQuery(); /* 删除表 */ InitBuildingList(); /* 更新列表 */ } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); cmdQ.Dispose(); /* 释放资源 */ conn.Close(); return(false); } cmdQ.Dispose(); /* 释放资源 */ conn.Close(); return(true); }
public async Task UpdateInstitute([FromBody] InstituteInfo instituteInfo, Guid instituteGuid) { await _institutesService.UpdateInstitute(instituteInfo, instituteGuid); }
public async Task <Guid> AddInstitute([FromBody] InstituteInfo instituteInfo) { return(await _institutesService.CreateInstitute(instituteInfo)); }