简体   繁体   中英

Select data from SQL based on specific values using C# perform operations

I need to select data saved on the basis of different ages, design alternatives and experiences in SQL using C#. The operations are performed in the data using for loop and the loop runs for all the IDs with the specified Age, Design alternative and experience. The code is attached. The results I am getting are the same for every age or design alternatives entered.

 public partial class Form3 : Form
{
    SqlCommand cnd;
    SqlConnection conn = new SqlConnection(@"Data Source=HAIER-PC;Initial Catalog=c_util;Integrated Security=True");
    int[] R = new int[10];
    double[] W = { 4.11, 2.21, 3.42, 3.01, 2.34, 4.10,3.03, 1.18, 1.99, 5.03 };
    double[] S = new double[10];
    double sum;
    double[] wp1= new double[10];
    double[] squ= new double[10];
    double[] l = new double[10];
    double learn = 0.5;
    int op;
    int ID;
    public Form3()
    {
        InitializeComponent(); 
    }
   private void button8_Click(object sender, EventArgs e)
    {

        int n;
        if (conn.State != ConnectionState.Open)
        conn.Open();
        cnd = new SqlCommand("select * from CDes where Age='" + comboBox1.Text + "' AND Exper='" + comboBox2.Text + "'AND Design='" + comboBox3.Text + "'  ", conn);
        SqlDataReader myreader = cnd.ExecuteReader();
        int[] R = new int[10];
            while (myreader.Read())
            {
                string id = myreader.GetInt32(0).ToString();
                string shell = myreader.GetInt32(4).ToString();
                string baseg = myreader.GetInt32(5).ToString();
                string vnt = myreader.GetInt32(6).ToString();
                string price = myreader.GetInt32(7).ToString();
                string impactliner = myreader.GetInt32(8).ToString(); ;
                string eyeport = myreader.GetInt32(9).ToString();
                string face = myreader.GetInt32(10).ToString();
                string comfort = myreader.GetInt32(11).ToString();
                string strap = myreader.GetInt32(12).ToString();
                string wgt = myreader.GetInt32(13).ToString();
                ID = Convert.ToInt32(id);
                R[0] = Convert.ToInt32(shell);
                R[1] = Convert.ToInt32(baseg);
                R[2] = Convert.ToInt32(vnt);
                R[3] = Convert.ToInt32(price);
                R[4] = Convert.ToInt32(impactliner);
                R[5] = Convert.ToInt32(eyeport);
                R[6] = Convert.ToInt32(face);
                R[7] = Convert.ToInt32(comfort);
                R[8] = Convert.ToInt32(strap);
                R[9] = Convert.ToInt32(wgt);
            }
            //Kohonen

             for ( n = 0; n <=ID; n++)

             {


            S[0] = R[0] - W[0];
            squ[0] = Math.Pow(S[0], 2);
            S[1] = R[1] - W[1];
            squ[1] = Math.Pow(S[1], 2);
            S[2] = R[2] - W[2];
            squ[2] = Math.Pow(S[2], 2);
            S[3] = R[3] - W[3];
            squ[3] = Math.Pow(S[3], 2);
            S[4] = R[4] - W[4];
            squ[4] = Math.Pow(S[4], 2);
            S[5] = R[5] - W[5];
            squ[5] = Math.Pow(S[5], 2);
            S[6] = R[6] - W[6];
            squ[6] = Math.Pow(S[6], 2);
            S[7] = R[7] - W[7];
            squ[7] = Math.Pow(S[7], 2);
            S[8] = R[8] - W[8];
            squ[8] = Math.Pow(S[8], 2);
            S[9] = R[9] - W[9];
            squ[9] = Math.Pow(S[9], 2);
            sum = squ[0] + squ[1] + squ[2] + squ[3] + squ[4] + squ[5] + squ[6] + squ[7] + squ[8] + squ[9];

            if (sum >= 75)
            {
                op = 1;
            }
            else
            {
                op = 0;
            }
            wp1[0] = W[0] + (learn * op * S[0]);
            wp1[1] = W[1] + (learn * op * S[1]);
            wp1[2] = W[2] + (learn * op * S[2]);
            wp1[3] = W[3] + (learn * op * S[3]);
            wp1[4] = W[4] + (learn * op * S[4]);
            wp1[5] = W[5] + (learn * op * S[5]);
            wp1[6] = W[6] + (learn * op * S[6]);
            wp1[7] = W[7] + (learn * op * S[7]);
            wp1[8] = W[8] + (learn * op * S[8]);
            wp1[9] = W[9] + (learn * op * S[9]);
            }

            myreader.Close();
            conn.Close();

        textBox13.Text = wp1[0].ToString();
        textBox14.Text =wp1[1].ToString();
        textBox15.Text = wp1[2].ToString();
        textBox20.Text = wp1[3].ToString();
        textBox16.Text = wp1[4].ToString();
        textBox21.Text = wp1[5].ToString();
        textBox17.Text = wp1[6].ToString();
        textBox22.Text = wp1[7].ToString();
        textBox18.Text = wp1[8].ToString();
        textBox23.Text = wp1[9].ToString();


    }

I think it is caused by the you do not use the loop internal n variable, so you always read the same data. Also you overwrites R table elements in every while loop iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM