2017-02-18 66 views
1

我正在嘗試使用C#WindowsForms應用程序中創建我的第一個員工管理器。我差不多完成了,但是顯示了一個我無法糾正的錯誤。不是所有的路徑返回值 - Windows Form App - C#

它是:

「CS0161 - 並非所有的代碼路徑返回一個值」

我試過,我覺得這裏的一切,但沒有結果......我要求你幫幫我!

namespace WPC 
{ 
public static class WPCDB 
{ 

    public static SqlConnection GetConnection() 
    { 
     string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c: \users\volko\onedrive\документи\visual studio 2015\Projects\WPC\WPC\WPC.mdf;Integrated Security=True"; 
     SqlConnection conn = new SqlConnection(connStr); 
     return conn; 
    } 
    public static void AddEmployee(string IDnumber, string firstName, string fatherName, string lastName, string departament, float salary, DateTime date) 
    { 
     string insStmt = "INSERT INTO TABLE EMPLOYEE (Idnumber, firstName, fatherName, lastName, department, salary, date) VALUES (@IDnumber, @firstName, @fatherName, @lastName, @department @salary, @date)"; 
     SqlConnection conn = GetConnection(); 
     SqlCommand insCmd = new SqlCommand(insStmt, conn); 
     insCmd.Parameters.AddWithValue("@IDnumber", IDnumber); 
     insCmd.Parameters.AddWithValue("@firstName", firstName); 
     insCmd.Parameters.AddWithValue("@fatherName", fatherName); 
     insCmd.Parameters.AddWithValue("@lastName", lastName); 
     insCmd.Parameters.AddWithValue("@department", departament); 
     insCmd.Parameters.AddWithValue("@department", salary); 
     insCmd.Parameters.AddWithValue("@department", date); 
     try 
     { 
      conn.Open(); insCmd.ExecuteNonQuery(); 
     } 
     catch (SqlException ex) { throw ex; } 
     finally { conn.Close(); } 
    } 

    public static List<Employee> GetEmployee() 

    { 
     List<Employee> employeelist = new List<Employee>(); 
     SqlConnection conn = GetConnection(); 
     string selStmt = "SELECT * FROM EMPLOYEE ORDER BY IDnumber, FirstName";   //employee table = table 
     SqlCommand selcmd = new SqlCommand(selStmt, conn); 
     try 
     { 
      conn.Open(); 
      SqlDataReader reader = selcmd.ExecuteReader(); 
      while (reader.Read()) 
      { 
       Employee employee = new Employee(); 
       employee.EmployeeNumber = (int)reader["EmployeeNumber"]; 
       employee.IDnumber = reader["IDnumber"].ToString(); 
       employee.FirstName = reader["FirstName"].ToString(); 
       employee.FatherName = reader["FatherName"].ToString(); 
       employee.LastName = reader["LastName"].ToString(); 
       employee.Depart = reader["Depart"].ToString(); 
       employee.Salary = (float)reader["Salary"]; 
       employee.Date = (DateTime)reader["Date"]; 

       employeelist.Add(employee); 

      } 
      reader.Close(); 

     } 
     catch (SqlException ex) { throw ex; } 
     finally { conn.Close(); } 

    }    
} 
} 

預先感謝您的時間!

回答

1

你的返回類型爲List<Employee>聲明的方法:

public static List<Employee> GetEmployee() 

在你的代碼不會從這個方法返回任何東西 - 你得到這個錯誤。

您應該返回的東西或與void返回類型聲明方法:

public static void GetEmployee() 

,我想你應該在try塊結束(reader.Close();後)有return聲明:

return employeelist; 
1

你方法有返回類型列表<>

public static List<Employee> GetEmployee() 

在列表<>塊添加此行末

return employeelist; 
相關問題