Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C使用多个数据库结果和列表框(当选择项时)在文本框中显示信息_C#_.net_Database_Logic - Fatal编程技术网

C# C使用多个数据库结果和列表框(当选择项时)在文本框中显示信息

C# C使用多个数据库结果和列表框(当选择项时)在文本框中显示信息,c#,.net,database,logic,C#,.net,Database,Logic,我正在使用数据库编写一本烹饪书。我一直在用教程来构建我的项目,因为我还是个新手 我以前问过如何使用按钮搜索数据库,而帮助太快了,我不得不问另一个问题 问题是: 我的表单允许用户查找含有特定成分的食谱。它允许他们在文本框中键入一种成分,按钮会在列表框中显示所有结果和配方名称。由于这里的帮助,该部分已成功编码。但是,一旦填充了列表框,我希望用户能够从列表框中选择配方,并且列表框旁边的文本框将填充特定配方的数据信息,例如配料、说明和其他注释 什么配方填充列表框取决于用户,所以如果没有一些严肃的逻辑,我

我正在使用数据库编写一本烹饪书。我一直在用教程来构建我的项目,因为我还是个新手

我以前问过如何使用按钮搜索数据库,而帮助太快了,我不得不问另一个问题

问题是:

我的表单允许用户查找含有特定成分的食谱。它允许他们在文本框中键入一种成分,按钮会在列表框中显示所有结果和配方名称。由于这里的帮助,该部分已成功编码。但是,一旦填充了列表框,我希望用户能够从列表框中选择配方,并且列表框旁边的文本框将填充特定配方的数据信息,例如配料、说明和其他注释

什么配方填充列表框取决于用户,所以如果没有一些严肃的逻辑,我就不能真正编写它,对吗

以下是我整个表单的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;


namespace Cookbook
{
    public partial class BrowseIngredients : Form
    {
        public BrowseIngredients()
        {
            InitializeComponent();
        }

        SqlConnection con;
        SqlDataAdapter dataAdapt;
        DataSet dataRecipe;
        int MaxRows = 0;
        int inc = 0;


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Exit Cook Book?", "Exit?", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            BrowseRecipes goBack = new BrowseRecipes();

            Close();
            goBack.Show();
        }

        private void howToSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("To look for recipes with ingredients you have, simply type in the first ingredient you have to cook. \r\n To narrow your search, add another ingredient you'd like to search for in the recipe results.", "Search by Ingredients");
        }


        private void BrowseIngredients_Load(object sender, EventArgs e)
        {

            con = new SqlConnection();
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Cady Wong\\My Documents\\Visual Studio 2010\\Projects\\Cookbook\\Cookbook\\Recipes.mdf;Integrated Security=True;User Instance=True";
            dataRecipe = new DataSet();

            con.Open();

           string sql = "SELECT* From CookBookRecipes";
           dataAdapt = new SqlDataAdapter(sql, con);
           dataAdapt.Fill(dataRecipe, "CookBookRecipes");
           NavigateRecords();
           MaxRows = dataRecipe.Tables["CookBookRecipes"].Rows.Count;

            con.Close();

        }

        private void NavigateRecords()
        {
            DataRow dRow = dataRecipe.Tables["CookBookRecipes"].Rows[inc];

        }


//This is the search and populate listbox code //


        private void btnSearch_Click(object sender, EventArgs e)
        {


            if (tbSearch.TextLength >= 1)
           {
                //MessageBox.Show("This will work when you put in a word!");

               listBox1.Items.Clear();

                string searchOut = tbSearch.Text;
                int result = 0;

                DataRow[] returnRows;

                returnRows = dataRecipe.Tables["CookBookRecipes"].Select("Recipe_Ingredients LIKE '*" + searchOut + "*'");


                result = returnRows.Length;

//This allows mutiple results to be seen one line after another //

                if (result > 0)
                {
                    string temp ="";
                    DataRow rowBack;
                    for (int index = 0; index < returnRows.Count(); index++ )
                    {
                        rowBack = returnRows[index];
                        listBox1.Items.Add(rowBack[0].ToString());
                        temp += rowBack[0].ToString();
                        temp += "\n";
                    }

                }
                else
                {
                    MessageBox.Show("No record");
                }

           }

           else
            {
               MessageBox.Show("Please enter an ingredient to search for!", "Search");
           }
        }

    }
}
如果你需要更多的信息,请告诉我


提前谢谢你

好吧,我还没有真正给出答案——更多的建议希望能推动你前进,不仅仅是在这个项目上,而是作为一名程序员

在我继续之前,请注意一点,对于您的问题,多个标记和逻辑是一个很好的选择

关于BrowseingElements\u Load:

这里您使用了一个实现接口的SqlConnection。最好的做法是,在处理实现IDisTable的对象时,至少在可能的情况下,始终使用

using (con = New SqlConnection(connectionString)){
    ' use the connection within these bracers
}
' because you used this pattern, the object is disposed correctly, memory deallocated, etc
下一点建议是看一下LINQtoSQLL2S。L2S是一种新型的传感器。听起来很复杂,但基本上意味着它负责将您的数据库对象表、视图等映射到您的域模型类、结构等。可能听起来也有点复杂,但相信我,看看这些非常简单的教程,您会喜欢它的:

注意:有很多可用的ORM,微软本身也提供实体框架,但L2S应该很容易学习,并为您提供足够的知识,以便您在将来对ORM替代方案做出明智的选择

如果您使用L2S,当您花了几个小时使用它时,您会发现每次它解决了一些其他问题时,您都会在代码中过度编写T-SQL,例如介绍Linq将为您处理的问题

它还可以解决您的总体问题,或者几乎解决。使用LINQtoSQL,显示配方数据会很简单,因为它们只是类的属性

txtIngredients.Text = myAutoGeneratedLinqRecipe.Ingredients;
我希望这能帮助你走上正轨,我相信其他人会给你代码来完成你的练习,而不会引入新的代码模式和技术^^

编辑:使用块的快速预览,让您了解:

Private void BrowseIngredients_Load(object sender, EventArgs e)
{
    string connStr = "Data Source=.....etc";
    using (con = New SqlConnection(connStr))
    {
        con.Open
        ' get your data here - if you were losing Linq, you wouldnt have to worry
        ' about connections or Sql at all ;-)
        con.Close
    } ' this ensures that the connection is disposed correctly, even if for example
      ' you throw an exception inside the block which isn't caught
}

因此,当用户单击列表框项时,您是否需要进行另一个数据库调用?比如从配方奶蛋饼的表格中选择*,或者类似的东西?@Nick:我想,因为所有的数据都在数据库中。列表框将由用户填充,因此我需要一些计算机逻辑来确定正在选择哪个配方,以任何排序或顺序,以及如何获取该配方并提供其其余细节。这有什么意义吗?非常感谢,我会看看那些教程。顺便说一句,我想不出我的问题中存在任何标签,所以有些标签有点奇怪。没问题@C Wong,我想你只要使用前3个C、.net数据库就可以了-这足够详细地描述了问题。如果你能看完这些教程中的任何一个,却不知道你自己问题的答案,我将用'con=New-sqlconnectionstring{connection-code}'来验证我的代码中的'using-system.Collections'是否直接在下面?好的,谢谢。我正试着对这件事保持清醒。使用LINQtoSQL,我可以重写我的整个代码,而不需要连接字符串、数据适配器、命令生成器或其他任何东西?当然可以。你有一个叫做数据模型的东西,它几乎就是你数据库的一张图片。L2S为您创建所需的所有类。如果您查看这些教程,您将看到如何使用DataContext,您需要一个conenction字符串,但在简单的环境中,linq可以将其保存在后台,这样您就不必将其保存在代码中,而代码将获取填充listview或文本框所需的对象,等等。检查一下,如果您有任何问题,请告诉我们。祝你好运