想实现的功能是输入图书编号和图书类型,查询数据库里是否存在相同的图书编号和图书类型。如果有相同的图书编号或没有图书类型,则不能插入图书。结果做出来不对,不管数据库里有没有图书编号,都显示的是"图书记录已存在,请重新输入!"。如果没有图书类型,只会报错不会显示"图书类型记录不存在,请重新输入!"
//插入新书目
public int Insert(int bookid, string name, int typeid, int number, float price)
{
try
{
if (GetBookID(bookid)!= null)
{
Console.WriteLine("图书记录已存在,请重新输入!");
return -1;
}
if (GetBookTypeByID(typeid) == null)
{
Console.WriteLine("图书类型记录不存在,请重新输入!");
return -1;
}
else
{
string sqltxt = string.Format("insert into Book([bookid],[name],[typeId],[number],[price]) values ('{0}','{1}','{2}','{3}','{4}')",
bookid, name, typeid, number, price);
SqlCommand com = new SqlCommand(sqltxt, con);
Open();
return (int)com.ExecuteNonQuery();
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return -1;
}
finally
{
Close();
}
} //查询数据库中是否存在相同的bookid
public SqlDataReader GetBookID(int bookid)
{
try
{
string sqltxt = string.Format("select * from Book where bookid='{0}'", bookid);
SqlCommand com = new SqlCommand(sqltxt, con);
Open();
SqlDataReader read = com.ExecuteReader();
read.Close();
return read;
}
finally
{
Close();
}
}
//查询数据库中是否有相同的图书类型编号
public SqlDataReader GetBookTypeByID(int typeID)
{
try
{
string sqltxt = string.Format("select * from BookType where typeId='{0}'", typeID);
SqlCommand com = new SqlCommand(sqltxt, con);
Open();
SqlDataReader read = com.ExecuteReader();
read.Close();
return read;
}
finally
{
Close();
}
}
//插入新书目
public int Insert(int bookid, string name, int typeid, int number, float price)
{
try
{
if (GetBookID(bookid)!= null)
{
Console.WriteLine("图书记录已存在,请重新输入!");
return -1;
}
if (GetBookTypeByID(typeid) == null)
{
Console.WriteLine("图书类型记录不存在,请重新输入!");
return -1;
}
else
{
string sqltxt = string.Format("insert into Book([bookid],[name],[typeId],[number],[price]) values ('{0}','{1}','{2}','{3}','{4}')",
bookid, name, typeid, number, price);
SqlCommand com = new SqlCommand(sqltxt, con);
Open();
return (int)com.ExecuteNonQuery();
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return -1;
}
finally
{
Close();
}
} //查询数据库中是否存在相同的bookid
public SqlDataReader GetBookID(int bookid)
{
try
{
string sqltxt = string.Format("select * from Book where bookid='{0}'", bookid);
SqlCommand com = new SqlCommand(sqltxt, con);
Open();
SqlDataReader read = com.ExecuteReader();
read.Close();
return read;
}
finally
{
Close();
}
}
//查询数据库中是否有相同的图书类型编号
public SqlDataReader GetBookTypeByID(int typeID)
{
try
{
string sqltxt = string.Format("select * from BookType where typeId='{0}'", typeID);
SqlCommand com = new SqlCommand(sqltxt, con);
Open();
SqlDataReader read = com.ExecuteReader();
read.Close();
return read;
}
finally
{
Close();
}
}
解决方案 »
- 今天写了一段代码,添加右键菜单。可是每点一次右键,就多出一个xianshi,我只想要一个xianshi,哪位大侠帮我解决一下,谢谢啦。急啊,今天就要交项目啊!!
- sql server生成的创建数据库脚本如何应用于access文件创建
- 关于数组的底层存储形式
- 求助!在什么情况下使用结构,什么情况下使用枚举
- sql2005 注册sql2000服务器
- 可不可以用C#重写用vb6.0写pos系统,请大家给点意见
- 一个编译过程产生的错误,请大家帮忙
- 安装的时候怎么把外来的com的ocx注册到系统里
- 请问如何在本机上获得服务器硬盘上的文件列表?
- 急急急急急。。。。,保证给分
- picturebox控件显示图片咋特别慢啊
- 我用vs2008部署项目的时候总是出现这个错误,请高手指点
比如//查询数据库中是否存在相同的bookid
public Boolean IsBookIDExist(int bookid) {
try {
string sqltxt = string.Format("select count(*) from Book where bookid='{0}'", bookid);
SqlCommand com = new SqlCommand(sqltxt, con);
Open();
int count = GetCount(com);
return count != 0;
} finally {
Close();
}
}
解决的方法可以参考dataxdata 这个哥们说的 定义个bool类型或者你可以看下你的read有没有count或者list之类的属性 通过count是否为0来判断数据库是否有值
{
try
{
string sqltxt = string.Format("select * from BookType where typeId='{0}'", typeID);
SqlCommand com = new SqlCommand(sqltxt, con);
Open();
SqlDataReader read = com.ExecuteReader();
read.Close();
return read.HasRows;
}
finally
{
Close();
} if (GetBookID(bookid)!= null)
改为if (GetBookID(bookid))