软件开发三Word文档格式.docx
《软件开发三Word文档格式.docx》由会员分享,可在线阅读,更多相关《软件开发三Word文档格式.docx(22页珍藏版)》请在冰豆网上搜索。
manger.InsertBook();
manger.UpdateBookPrice();
manger.DeleteBook();
Console.ReadLine();
}
}
usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
classLibraryManager
privateDBOperation_dbOper=newDBOperation();
#region执行验证用户并处理结果信息
publicvoidLogin()
Console.WriteLine("
请输入用户名"
);
stringstrUserName=Console.ReadLine();
请输入密码"
stringstrPwd=Console.ReadLine();
stringstrMsg=string.Empty;
boolbRet=_dbOper.CheckUserInfo(strUserName,strPwd,refstrMsg);
if(bRet)
登录成功"
ShowMenu();
else
登录失败"
+strMsg);
#endregion
#region菜单显示
publicvoidShowMenu()
stringoption="
"
;
do
Console.WriteLine();
============请选择操作键=========="
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输出图书列表
publicvoidShowLibraryList()
StringBuildersbtext=newStringBuilder();
SqlDataReaderreader=_dbOper.GetLibraryList();
if(reader==null)
出现异常"
return;
-----------------------------------------------------"
编号\t名称\t\t类别\t\t数量\t价格"
while(reader.Read())
sbtext.AppendFormat("
{0}\t{1}\t{2}\t{3}\t{4}"
reader["
id"
],reader["
name"
typeName"
number"
price"
]);
Console.WriteLine(sbtext);
sbtext.Length=0;
------------------------------------------------------"
reader.Close();
#region插入图书信息
publicvoidInsertBook()
intbookID,bookNum,typeID=0;
StringbookName;
doublebookPrice;
请输入图书编号"
bookID=int.Parse(Console.ReadLine());
SqlDataReaderreader=_dbOper.GetBookByID(bookID);
if(reader.Read())
编号是{0}的图书已经存在,请重新输入!
bookID);
请输入图书名称"
bookName=Console.ReadLine();
boolflag=false;
请输入图书类型编号"
typeID=int.Parse(Console.ReadLine());
SqlDataReaderrdType=_dbOper.GetBookTypeByID(typeID);
if(rdType.Read())
flag=true;
//Console.WriteLine("
{0}"
typeId"
//Console.WriteLine("
{0}\t{1}"
编号是{0}的图书不存在,请重新输入!
typeID);
rdType.Close();
}while(flag==false);
请输入图书数量"
bookNum=int.Parse(Console.ReadLine());
请输入图书单价"
bookPrice=double.Parse(Console.ReadLine());
intiRet=_dbOper.InsertBook(bookID,bookName,typeID,bookNum,bookPrice);
if(iRet==-1)
出现异常"
插入成功"
#region更新图书记录
publicvoidUpdateBookPrice()
try
intbookID=int.Parse(Console.ReadLine());
请输入修改后的价格"
doubleprice=double.Parse(Console.ReadLine());
intiRet=_dbOper.UpdateBookPriceByID(bookID,price);
异常发生"
修改成功"
catch(Exception)
输入错误"
#region删除图书记录
publicvoidDeleteBook()
没有找到图书编号是{0}的图书记录"
bookID);
确定删除这条图书记录么?
(Y/N)"
if(Console.ReadLine().Trim().ToUpper()!
="
Y"
)
退出删除图书记录操作!
intiRet=_dbOper.DeleteBookByID(bookID);
删除失败!
删除成功!
classDBOperation
privateconststringstrConn=@"
DataSource=.;
InitialCatalog=Library;
UserID=sa;
Pwd=sa"
#region检查用户信息
publicboolCheckUserInfo(stringuserName,stringpwd,refstringstrMsg)
SqlConnectionconn=newSqlConnection(strConn);
stringstrSql="
selectcount(*)from[User]whereloginId='
+userName+"
'
andloginPwd='
+pwd+"
conn.Open();
SqlCommandcomm=newSqlCommand(strSql,conn);
intiRet=(int)comm.ExecuteScalar();
if(iRet!
=1)
strMsg="
输入无效!
returnfalse;
returntrue;
catch(Exceptione)
发生异常!
+e.Message;
finally
conn.Close();
#region测试Library数据库连接
publicvoidTestConnectDB()
stringconnString="
pwd=sa"
SqlConnectionconnection=newSqlConnection(connString);
connection.Open();
连接成功!
catch(Exceptionex)
+ex.Message);
connection.Close();
关闭和数据库连接成功!
#region取得图书列表
publicSqlDataReaderGetLibraryList()
StringBuildersb=newStringBuilder();
sb.AppendLine("
SELECT"
A.[id]"
A.[name]"
B.[typeName]"
A.[number]"
A.[price]"
FROM"
[Book]asA,[BookType]asB"
WHERE"
A.[typeId]=B.[typeId]"
SqlCommandcomm=newSqlCommand(sb.ToString(),conn);
returncomm.ExecuteReader();
returnnull;
#region按图书编号查询记录
publicSqlDataReaderGetBookByID(intbookID)
SELECT"
a.[id]"
a.[name]"
b.[typename]"
a.[number]"
a.[price]"
[Book]a,[BookType]b"
a.[typeId]=b.[typeId]"
);
AND"
[Id]="
+bookID);
Console.WriteLine(e.Message);
}
#region按类型编号查询图书记录
publicSqlDataReaderGetBookTypeByID(inttypeID)
[typeId]"
[typeName]"
[BookType]"
[typeId]="
+typeID);
#region插入图书记录
publicintInsertBook(intbookID,stringbookName,inttypeID,intbookNum,doublebookPrice)
INSERTINTO[Book]"
VALUES"
('
+bookID+"
"
'
+bookName+"
+typeID+"
+bookNum+"
+bookPrice+"
)"
returncomm.ExecuteNonQuery();
return-1;
#region插入信息
publicintInsertType(stringtypeName)
INSERTINTO"
[type]"
+typeName+"
#region修改图书价格信息
publicintUpdateBookPriceByID(intbookID,doublebookPrice)
StringBuilder