C# 需要计算两个整数值的总和

C# 需要计算两个整数值的总和,c#,ado.net,int,C#,Ado.net,Int,您需要从方法中返回一些内容: private void cbService1_SelectedIndexChanged(object sender, EventArgs e) { string str = "Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=Salon Primik; Integrated Security = True"; SqlConnection cn = new

您需要从方法中返回一些内容:

private void cbService1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = "Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=Salon Primik; Integrated Security = True";
            SqlConnection cn = new SqlConnection(str);

            string Name = cbService1.SelectedValue.ToString();
            cn.Open();
            string Sql = "select Amount from Service where Name='" + Name + "'";

            SqlCommand cmd = new SqlCommand(Sql, cn);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                lblService1.Text = dr["Amount"].ToString();
                int i1 = Convert.ToInt32(lblService1.Text);
                total(i1,0);

            }
            dr.Close();
            cn.Close();
        }

private void cbService2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = "Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=Salon Primik; Integrated Security = True";
            SqlConnection cn = new SqlConnection(str);

            string Name = cbService2.SelectedValue.ToString();
            cn.Open();
            string Sql = "select Amount from Service where Name='" + Name + "'";

            SqlCommand cmd = new SqlCommand(Sql, cn);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                lblService2.Text = dr["Amount"].ToString();
                int i2 = Convert.ToInt32(lblService2.Text);
                total(0,i2);
            }
            dr.Close();
            cn.Close();
        }

        private int total(int i,int i2)
        {
            int Total = i + i2;
            string total = Total.ToString();
            lblTotal.Text = total;
而这一切都开始了

private int total(int i,int i2)
{
    int Total = i + i2;
    return Total;
}
或者,如果您希望在方法中更改标签而不返回任何内容,请将其更改为

private void cbService2_SelectedIndexChanged(object sender, EventArgs e)
{
    // your other code
    lblTotal.Text = total(i1, i2).ToString();
}

在这两个事件上分别编辑您的代码。

lblService1.Text=dr[“Amount”].ToString();
int i1=Convert.ToInt32(lblService1.Text);
int i2=Convert.ToInt32(lblService2.Text);
总数(i1、i2);
lblService2.Text=dr[“Amount”].ToString();
int i1=Convert.ToInt32(lblService1.Text);
int i2=Convert.ToInt32(lblService2.Text);
总数(i1、i2);

SQL SUM()函数也是一个很好的尝试。

这里的问题是什么?我需要计算两个int值(i和i2)。int i1=Convert.ToInt32(lblService1.Text);int i2=Convert.ToInt32(lblService2.Text);需要添加,并且必须计算总数。i2在cbService1\u SelectedIndexChanged方法中可用。并且仍然无法计算.In lblTotal.Text=total(i1,i2).ToString();i1和i2用于标记需要作为参数传递给total()方法的2个整数值。这是基本的东西,也许你应该读一些教程。
private void total (int i1, int i2)