首页 > 解决方案 > 使用 C# 执行操作根据特定值从 SQL 中选择数据

问题描述

我需要根据不同年龄、设计方案和使用C#的SQL经验选择保存的数据。这些操作是使用 for 循环在数据中执行的,并且循环针对具有指定年龄、设计备选方案和经验的所有 ID 运行。附上代码。对于输入的每个年龄或设计替代方案,我得到的结果都是相同的。

 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();


    }

标签: c#sql

解决方案


我认为这是由于您没有使用循环内部 n 变量造成的,因此您总是读取相同的数据。您还会在每个 while 循环迭代中覆盖 R 表元素。


推荐阅读