1、 manger.InsertBook(); manger.UpdateBookPrice(); manger.DeleteBook(); Console.ReadLine(); using System;using System.Data;using System.Data.SqlClient; class LibraryManager private DBOperation _dbOper = new DBOperation(); #region 执行验证用户并处理结果信息 public void Login() Console.WriteLine(请输入用户名); string strUs
2、erName = Console.ReadLine();请输入密码 string strPwd = Console.ReadLine(); string strMsg = string.Empty; bool bRet = _dbOper.CheckUserInfo(strUserName, strPwd, ref strMsg); if (bRet)登录成功 ShowMenu(); else登录失败 + strMsg); #endregion #region 菜单显示 public void ShowMenu() string option = ; do Console.WriteLine(
3、);=请选择操作键=1.查看全部图书信息”);2.插入图书信息3.修改图书信息4.删除图书信息5.退出= option = Console.ReadLine(); switch (option) case 1: ShowLibraryList(); continue;2 InsertBook();3 UpdateBookPrice();4 DeleteBook();5 break; default: while (true); #region 输出图书列表 public void ShowLibraryList() StringBuilder sbtext = new StringBuilde
4、r(); SqlDataReader reader = _dbOper.GetLibraryList(); if (reader = null)出现异常 return;-编号t名称tt类别tt数量t价格 while (reader.Read() sbtext.AppendFormat(0t1t2t3t4, readerid, readernametypeNamenumberprice); Console.WriteLine(sbtext); sbtext.Length = 0;- reader.Close(); #region 插入图书信息 public void InsertBook() i
5、nt bookID, bookNum, typeID = 0; String bookName; double bookPrice;请输入图书编号 bookID = int.Parse(Console.ReadLine(); SqlDataReader reader = _dbOper.GetBookByID(bookID); if (reader.Read()编号是0的图书已经存在,请重新输入!, bookID);请输入图书名称 bookName = Console.ReadLine(); bool flag = false;请输入图书类型编号 typeID = int.Parse(Cons
6、ole.ReadLine(); SqlDataReader rdType = _dbOper.GetBookTypeByID(typeID); if (rdType.Read() flag = true; /Console.WriteLine(0typeId / Console.WriteLine(0t1编号是0的图书不存在,请重新输入!, typeID); rdType.Close(); while (flag = false);请输入图书数量 bookNum = int.Parse(Console.ReadLine();请输入图书单价 bookPrice = double.Parse(Co
7、nsole.ReadLine(); int iRet = _dbOper.InsertBook(bookID, bookName, typeID, bookNum, bookPrice); if (iRet = -1)出现异常 插入成功 #region 更新图书记录 public void UpdateBookPrice() try int bookID = int.Parse(Console.ReadLine();请输入修改后的价格 double price = double.Parse(Console.ReadLine(); int iRet = _dbOper.UpdateBookPri
8、ceByID(bookID, price);异常发生修改成功 catch (Exception)输入错误 #region 删除图书记录 public void DeleteBook()没有找到图书编号是0的图书记录,bookID);确定删除这条图书记录么?(Y/N) if (Console.ReadLine().Trim().ToUpper() != Y)退出删除图书记录操作! int iRet = _dbOper.DeleteBookByID(bookID);删除失败!删除成功! class DBOperation private const string strConn = Data So
9、urce=.;Initial Catalog=Library;User ID=sa;Pwd=sa #region 检查用户信息 public bool CheckUserInfo(string userName, string pwd, ref string strMsg) SqlConnection conn = new SqlConnection(strConn); string strSql = select count(*) from User where loginId= + userName + and loginPwd= + pwd + conn.Open(); SqlComma
10、nd comm = new SqlCommand(strSql, conn); int iRet = (int)comm.ExecuteScalar(); if (iRet != 1) strMsg = 输入无效! return false; return true; catch (Exception e)发生异常! + e.Message; finally conn.Close(); #region 测试Library数据库连接 public void TestConnectDB() string connString = pwd=sa SqlConnection connection =
11、new SqlConnection(connString); connection.Open();连接成功! catch (Exception ex) + ex.Message); connection.Close();关闭和数据库连接成功! #region 取得图书列表 public SqlDataReader GetLibraryList() StringBuilder sb = new StringBuilder(); sb.AppendLine(SELECT A.id ,A.name ,B.typeName ,A.number ,A.priceFROM Book as A ,BookT
12、ype as B WHERE A.typeId = B.typeId SqlCommand comm = new SqlCommand(sb.ToString(), conn); return comm.ExecuteReader(); return null; #region 按图书编号查询记录 public SqlDataReader GetBookByID(int bookID)SELECT a.id ,a.name ,b.typename ,a.number ,a.price Book a, BookType b a.typeId = b.typeId );AND Id = + boo
13、kID); Console.WriteLine(e.Message); #region 按类型编号查询图书记录 public SqlDataReader GetBookTypeByID(int typeID) typeId ,typeName BookType typeId = + typeID); #region 插入图书记录 public int InsertBook(int bookID, string bookName, int typeID, int bookNum, double bookPrice)INSERT INTO Book VALUES ( + bookID + , + bookName + + typeID + + bookNum + + bookPrice + ) return comm.ExecuteNonQuery(); return -1; #region 插入信息 public int InsertType(string typeName)INSERT INTO type + typeName + #region 修改图书价格信息 public int UpdateBookPriceByID(int bookID, double bookPrice) StringBuilder
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1