想实现的功能是输入图书编号和图书类型,查询数据库里是否存在相同的图书编号和图书类型。如果有相同的图书编号或没有图书类型,则不能插入图书。结果做出来不对,不管数据库里有没有图书编号,都显示的是"图书记录已存在,请重新输入!"。如果没有图书类型,只会报错不会显示"图书类型记录不存在,请重新输入!"
//插入新书目
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();
}
}
你确定如果查不着,com.ExecuteReader() 返回值是null?
我没用过SqlDataReader。但估计是 if (GetBookID(bookid)!= null) 判断,不在点上。
估计应该判断GetBookID(bookid) 的返回值,是否可以下移。
public int Insert(int bookid, string name, int typeid, int number, float price)
{
try
{
if (GetBookID(bookid)!= null)//这里打断点F11调试 调试进入方法GetBookID(int bookid) 在查看方法中 SqlDataReader read = com.ExecuteReader(); read的值 你的错误显然是说明这里的read并非为null
{
Console.WriteLine("图书记录已存在,请重新输入!");
return -1;
}