当我试图返回某些内容时,并非所有代码路径都返回值错误

时间:2016-05-07 20:37:22

标签: c#

**为什么我收到此错误(并非所有代码路径都返回值)     当我试图在函数中返回一些东西时我该如何解决它 我需要返回值作为以下代码,但我无法做到这一点,因为这个错误可以帮助我**

public int return_order(string product_name)
{
    FileStream f1 = new FileStream("name.txt", FileMode.Open);
    StreamReader r1 = new StreamReader(f1);
    string name = r1.ReadLine();
    r1.Close();
    SqlConnection s = new SqlConnection("Data Source=(local);Initial Catalog=ShoppingOnline;Integrated Security=True");
    s.Open();
    SqlCommand d = new SqlCommand("Select * from Products where ProductName ='" + product_name + "'", s);
    SqlDataReader dr;
    dr = d.ExecuteReader();
    int i = 0;
    while (dr.Read())
    {
        i += 1;
    }
    dr.Close();
    if (i == 1)
    {
        Form3 f = new Form3();
        SqlCommand cmd = new SqlCommand("UPDATE Products SET Quantity = Quantity + 1 WHERE ProductName= @username ", s);
        cmd.Parameters.AddWithValue("@username", product_name);
        cmd.ExecuteNonQuery();
    }
    else
    {
        DateTime today = DateTime.Today;
        DateTime startdate = DateTime.UtcNow.Date;
        FileStream f = new FileStream("bill.txt", FileMode.Open);
        StreamReader r = new StreamReader(f);
        string q = r.ReadToEnd();
        string w = "";

        if (q.Contains(product_name))
        {
            while(r.ReadLine()!="EOO")
            {
                f.Position = 0;
                r.DiscardBufferedData();
                string enddate=r.ReadLine();
                f.Position = 0;
                r.DiscardBufferedData();
                while (r.Peek() != -1)
                {
                    DateTime dt = Convert.ToDateTime(enddate);  
                    DateTime dt2 = Convert.ToDateTime(startdate);

                    if ((dt2 - dt.Date).TotalDays < 14)
                    {
                        string temp = r.ReadLine();
                        if (temp == "Customer Name: " + name + "")
                        {
                            string record = r.ReadLine();
                            string[] fields = record.Split('@');
                            if (fields[0] == product_name)
                            {
                                w = fields[1];
                                SqlCommand cmd = new SqlCommand("insert into Products(ProductName,Price,Quantity) values('" + product_name + "','" + w + "','" + 1 + "')", s);
                                cmd.ExecuteNonQuery();
                                return 1;
                                break;
                            }
                        }
                    }
                    else 
                    { 
                        return -1; 
                    }//late
                }
                r.Close();
            }
        }
        else 
        { 
            return 2; 
        }//wrong
    }
}

1 个答案:

答案 0 :(得分:0)

我已使用提前退货简化了您的代码。并找到两个未定义的路径(见注释)。

我没有分析这段代码。只是通过扭转条件进行机械转换。 (顺便说一句需要认真审查代码)

var http = require('http');

var map = require('through2-map');

uc = map(function(ch) {    
  return ch.toString().toUpperCase();  
});

server = http.createServer(function(request, response) {
  request.on('data',function(chunk){
      if (request.method == 'POST') {
         //change the data from request to uppercase letters and 
         //pipe to response. 
      }
    });
});
server.listen(8000);