想实现的功能是输入图书编号和图书类型,查询数据库里是否存在相同的图书编号和图书类型。如果有相同的图书编号或没有图书类型,则不能插入图书。结果做出来不对,不管数据库里有没有图书编号,都显示的是"图书记录已存在,请重新输入!"。如果没有图书类型,只会报错不会显示"图书类型记录不存在,请重新输入!"
//插入新书目
        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();
           }
           
       }

解决方案 »

  1.   

    没用过C#,感觉GetBookID和GetBookTypeByID两个函数改名为IsBookIdExist和IsBookTypeExist,并返回Boolean类型更合适些
    比如//查询数据库中是否存在相同的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();
        }
    }
      

  2.   

    public SqlDataReader GetBookID(int bookid)这个方法 返回的永远是一个SqlDataReader类型的变量  也就是你定义的read   所以 他永远不会==null
    解决的方法可以参考dataxdata 这个哥们说的  定义个bool类型或者你可以看下你的read有没有count或者list之类的属性 通过count是否为0来判断数据库是否有值
      

  3.   

    public bool 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.HasRows;
        
      }
      finally
      {
      Close();
      } if (GetBookID(bookid)!= null)
    改为if (GetBookID(bookid))