【发布时间】:2011-07-12 14:24:28
【问题描述】:
我正在使用数据库编写一本烹饪书。因为我还是个新手,所以我一直在使用教程来构建我的项目。
我之前问过如何使用按钮搜索数据库,帮助太快了,我得再问一个问题。
问题来了:
我的表单允许用户查找具有特定成分的食谱。它允许他们在文本框中键入一种成分,并且该按钮在列表框中显示所有结果(食谱的名称)。由于此处的帮助,该部分已成功编码。但是,一旦填充了列表框,我希望用户能够从列表框中选择一个食谱,并且列表框旁边的文本框会填充特定食谱的数据信息(例如成分、方向和其他厘米)。
填充列表框的配方取决于用户,所以如果没有一些严肃的逻辑,我真的无法编写代码,对吧?
这是我整个表单的代码:
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");
}
}
}
}
如果您需要更多信息,请告诉我!
提前谢谢你!
【问题讨论】:
-
那么当用户点击一个列表框项目时,你需要进行另一个数据库调用吗?像
select * from table where recipe='quiche'或类似的东西? -
@Nick:我想,因为所有数据都在数据库中。列表框将由用户填充,因此我需要一些计算机逻辑来确定选择了哪个菜谱,以任何排序或顺序,以及如何获取该菜谱并提供其其余详细信息。这有意义吗?