如何在 C# 程序中工作?

Posted

技术标签:

【中文标题】如何在 C# 程序中工作?【英文标题】:How do you get WHERE to work in a C# program? 【发布时间】:2021-12-20 22:23:48 【问题描述】:

我不知道为什么我的WHERE 不会给我所有东西。我目前正在尝试为我的公司制作一个数据库接口程序,以便我们可以摆脱现有的。现在我承认我已经有一段时间没有对数据库做过任何工作了,所以我有点生疏了。无论如何,我的程序的一部分将用于搜索数据库,因此它可以正常搜索,但是当我尝试让它执行 WHERE 时,它只会给我一个名字(如果有的话)。我一直在摆弄代码,尝试在输出中添加另一个 dataReader.GetName()dataReader.GetValue() 之类的部分,但它只是对我大惊小怪,不喜欢它。

我不确定它在代码中的位置是否有问题,或者我没有要求它做正确的事情。我所知道的是,我很可能错过了一些真正的小东西。这是我拥有的代码,我认为应该为我提供WHERE 提取的所有内容的所有名称:

while (dataReader.Read())

    Output = Output + dataReader.GetName(0) + ": " + 
    dataReader.GetValue(0);
    Output = Output + "\n";

但是,它只给了我一个,而且它是列表最后一个要启动的。我尝试使用变量增加 dataReaders 中的数字,但是这样做时它使我的索引超出范围。是否有可能在最后开始阅读?这是完整的代码,对长度表示歉意,处理输出:

    public static string LowerFirst(string value)
     
        // This is so strings can be converted to camelCase format if they need to be.
        if (value.Length > 0)
        
            var letters = value.ToCharArray();
            letters[0] = char.ToLowerInvariant(letters[0]);
            return new string(letters);
        
        return value;
    

    private void button1_Click(object sender, EventArgs e)
    
        SqlConnection cnn;
        cnn = new SqlConnection(@"Data Source=DESKTOP\SQLEXPRESS; Initial Catalog=M;Integrated Security=SSPI");
        cnn.Open();

        SqlCommand command;
        SqlDataReader dataReader;
        SqlDataAdapter adapter = new SqlDataAdapter();
        string sql = "Select ";
        string Output = "";
        string b1 = LowerFirst(columnBox1.Text.Replace(" ", ""));
        string b2 = LowerFirst(columnBox2.Text.Replace(" ", ""));
        string b3 = LowerFirst(columnBox3.Text.Replace(" ", ""));
        string b4 = LowerFirst(columnBox4.Text.Replace(" ", ""));
        string b5 = LowerFirst(columnBox5.Text.Replace(" ", ""));
        string tabl = tableGroupbox.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked).Text;
        string wer1 = LowerFirst(columnChoice2.SelectedItem.ToString().Replace(" ", ""));
        string wer2 = conditionTxtbox.Text;
        string wer3 = LowerFirst(columnChoice1.SelectedItem.ToString().Replace(" ", ""));
        int c = 0;
        int l = 5;
        int serNum = 0;
        int chCount = 0;


        if (everythingBox.Checked == true)
        
          // Resets the check boxes in case.
            if (columnBox1.Checked == true) columnBox1.Checked = false; 
            if (columnBox2.Checked == true) columnBox2.Checked = false;
            if (columnBox3.Checked == true) columnBox3.Checked = false;
            if (columnBox4.Checked == true) columnBox4.Checked = false;
            if (columnBox5.Checked == true) columnBox5.Checked = false;
            sql = sql + "*"; 
        
        else if (tableName1Radiobutton.Checked == true) sql = sql + "invoiceID"; else if (tableName2Radiobutton.Checked == true) sql = sql + "customerID"; else if (tableName3Radiobutton.Checked == true) sql = sql + "itemID";
       
        //Sets up order and what is counted in regards to spaces.
        if (columnBox1.Checked == true) sql = sql + "," + b1; chCount = chCount + 1;
        if (columnBox2.Checked == true && chCount != 0)
        
            sql = sql + "," + b2;
            chCount++;
                  
        else if (columnBox2.Checked == true && chCount == 0)
        
            sql += b2;
            chCount++;
        

        if (columnBox3.Checked == true && chCount != 0)
        
            sql = sql + "," + b3;
            chCount++;
        
        else if (columnBox3.Checked == true && chCount == 0)
        
            sql = sql + b3;
            chCount++;
        
        if (columnBox4.Checked == true && chCount != 0)
        
            sql = sql + "," + b4;
            chCount++;
        
        else if (columnBox4.Checked == true && chCount == 0)
        
            sql = sql + b4;
            chCount++;
        
        if (columnBox5.Checked == true && chCount != 0) sql = sql + "," + b5; else if (columnBox5.Checked == true) sql = sql + b5;

        MessageBox.Show(chCount.ToString());

        sql = sql + " from " + tabl;

        if (whereChkbox.Checked == true)
        
            // Set up to perform a targeted search. However.... 
            MessageBox.Show(wer1);
            sql = "SELECT * FROM " + tabl + " WHERE " + wer1 + "='" + wer2 + "'"; 

        

        // Used to tell what boxes have been checked. Now rendered moot by other code?
        if (columnBox1.Checked == true) serNum = serNum + 1;
        if (columnBox2.Checked == true) serNum = serNum + 2;
        if (columnBox3.Checked == true) serNum = serNum + 4;
        if (columnBox4.Checked == true) serNum = serNum + 8;
        if (columnBox5.Checked == true) serNum = serNum + 16;
        if (everythingBox.Checked == true) serNum = 0;
       
        // The l would tell the output how far to go with the help of a c int. 
        if (serNum == 1 || serNum == 2 || serNum == 4 || serNum == 8 || serNum == 16) l = 2;
        if (serNum == 3 || serNum == 5 || serNum == 9 || serNum == 17 || serNum == 6 || serNum == 10 || serNum == 18 || serNum == 12 || serNum == 20 || serNum == 24) l = 3;
        if (serNum == 7 || serNum == 11 || serNum == 19 || serNum == 13 || serNum == 21 || serNum == 25 || serNum == 14 || serNum == 22 || serNum == 28 || serNum == 26) l = 4;
        if (serNum == 0 || serNum == 15 || serNum == 23 || serNum == 29 || serNum == 27 || serNum == 30) l = 5;
        if (serNum == 31) l = 6;
        if (serNum == 0 && columnBox5.Checked == true) l = 6;
        MessageBox.Show(sql,serNum.ToString());

        command = new SqlCommand(sql, cnn);
        dataReader = command.ExecuteReader();

        // Controls how far the dataReader reads to prevent out of bounds on the index. 
        while (dataReader.Read() && whereChkbox.Checked == false)
        
            Output = Output + " " + dataReader.GetName(0) + ": " + dataReader.GetValue(0);
            
            if (everythingBox.Checked == true)
            
                if (tableName3Radiobutton.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2) + " " + dataReader.GetName(3) + ": " + dataReader.GetValue(3) + " " + dataReader.GetName(4) + ": " + dataReader.GetValue(4) + " " + dataReader.GetName(5) + ": " + dataReader.GetValue(5);
                else Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2) + " " + dataReader.GetName(3) + ": " + dataReader.GetValue(3) + " " + dataReader.GetName(4) + ": " + dataReader.GetValue(4);
            
            else if (columnBox5.Checked == true)
            
                if (columnBox1.Checked == true && columnBox2.Checked == true && columnBox3.Checked == true && columnBox4.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2) + " " + dataReader.GetName(3) + ": " + dataReader.GetValue(3) + " " + dataReader.GetName(4) + ": " + dataReader.GetValue(4) + " " + dataReader.GetName(5) + ": " + dataReader.GetValue(5); 
                else if(columnBox1.Checked == true && columnBox2.Checked == true && columnBox3.Checked == true || columnBox1.Checked == true && columnBox3.Checked == true && columnBox4.Checked == true || columnBox1.Checked == true && columnBox2.Checked == true && columnBox4.Checked == true || columnBox2.Checked == true && columnBox3.Checked == true && columnBox4.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2) + " " + dataReader.GetName(3) + ": " + dataReader.GetValue(3) + " " + dataReader.GetName(4) + ": " + dataReader.GetValue(4);
                else if (columnBox1.Checked == true && columnBox2.Checked == true || columnBox1.Checked == true && columnBox3.Checked == true || columnBox1.Checked == true && columnBox4.Checked == true || columnBox2.Checked == true && columnBox3.Checked == true || columnBox2.Checked == true && columnBox4.Checked == true || columnBox3.Checked == true && columnBox4.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2) + " " + dataReader.GetName(3) + ": " + dataReader.GetValue(3);
                else if (columnBox1.Checked == true || columnBox2.Checked == true || columnBox3.Checked == true || columnBox4.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2);
                else Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1);
            
            else if (columnBox4.Checked == true)
            
                if (columnBox1.Checked == true && columnBox2.Checked == true && columnBox3.Checked == true || columnBox1.Checked == true && columnBox3.Checked == true && columnBox5.Checked == true || columnBox2.Checked == true && columnBox3.Checked == true && columnBox5.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2) + " " + dataReader.GetName(3) + ": " + dataReader.GetValue(3) + " " + dataReader.GetName(4) + ": " + dataReader.GetValue(4);
                else if (columnBox1.Checked == true && columnBox2.Checked == true || columnBox1.Checked == true && columnBox3.Checked == true || columnBox1.Checked == true && columnBox5.Checked == true || columnBox2.Checked == true && columnBox3.Checked == true || columnBox2.Checked == true && columnBox5.Checked == true || columnBox3.Checked == true && columnBox5.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2) + " " + dataReader.GetName(3) + ": " + dataReader.GetValue(3);
                else if (columnBox1.Checked == true || columnBox2.Checked == true || columnBox3.Checked == true || columnBox5.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2);
                else Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1);
            
            else if (columnBox3.Checked == true)
            
              if (columnBox1.Checked == true && columnBox2.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2) + " " + dataReader.GetName(3) + ": " + dataReader.GetValue(3); 
              else if (columnBox1.Checked == true || columnBox2.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2); 
              else Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1);
            
            else if (columnBox2.Checked == true)
            
                if (columnBox1.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1) + " " + dataReader.GetName(2) + ": " + dataReader.GetValue(2); else Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1);
            
            else if (columnBox1.Checked == true) Output = Output + " " + dataReader.GetName(1) + ": " + dataReader.GetValue(1);
            Output = Output + "\n"; 
            
        

        //Should give you the results of a targeted search. However it's currently not wanting to do so. 
        if (whereChkbox.Checked == true)
        
            while (dataReader.Read())
           
            Output = Output + dataReader.GetName(0) + ": " + dataReader.GetValue(0);
            Output = Output + "\n";

           

         
        dataReader.Close();
        command.Dispose();

        richTextBox1.Text = Output;
        if (seprateWindbox.Checked == true) MessageBox.Show(Output);
        cnn.Close();
    

如果您需要我提供更多/更好的信息,请告诉我,并提前感谢您的帮助。这是程序的图片,带有显示正在进入其中的 sql 的消息框,以防万一。

【问题讨论】:

嗯,我的 glasorb 抱怨这里的信息有限。代码太少。也许您缺少一些读取几乎所有结果的代码,或者 WHERE 子句已关闭。也许尝试在某些前端执行 SQL 1:1。 请包含您的实际 SQL 语句。您使用的是什么数据库系统?如果直接在数据库上执行语句会发生什么? 认为你应该从 ORM 开始,如果需要,可以向后工作 您仍然没有最重要的部分,即在哪里填充数据读取器以及如何构造 SQL 语句。请使用比“checkbox8”更有意义的字段名称,我们或您项目中的任何未来开发人员如何知道它们代表什么。请记住,数据读取器只能读取一次。 @JonP 我在其余代码中添加并更改了名称以希望更好地反映正在发生的事情。让我知道我是否需要对它做任何其他事情。 【参考方案1】:

重新考虑您的 while 循环。当您到达目标搜索时,dataReader.Read() 将被调用两次。

我会选择以下内容:

while (dataReader.Read())

   if (whereChkbox.Checked)
   
      Output = Output + dataRead
   
   else
   
      //Alternate logic here
   

或者将第一个 while 语句重新排序为

while (whereChkbox.Checked == false && dataReader.Read())

    //logic here

这样如果复选框被选中,下面的data.Read()就不会发生逻辑short circuiting

【讨论】:

工作就像一个魅力,谢谢!

以上是关于如何在 C# 程序中工作?的主要内容,如果未能解决你的问题,请参考以下文章

数组语义初始化器如何在 C# 中工作?

Freetext() 如何在索引服务中工作?

While 循环计时如何在带有多个线程的 C# 中工作?

如何让我的命令在 Discord.Net 中工作

如何让我的存储过程在我的 Visual Studio 项目中工作?

为啥在为接口创建实例时它可以在 C# 中工作?