简体   繁体   English

如何从我的 PostgreSQL 数据库中检索数据? WPF/C#

[英]How can I retrieve data from my PostgreSQL database? WPF/C#

For reference, I am new to C#/WPF/PostgreSQL and I am trying to create a project for practice, however I've hit a bit of a roadblock.作为参考,我是 C#/WPF/PostgreSQL 的新手,我正在尝试创建一个项目进行练习,但是我遇到了一些障碍。 I found this earlier and tried following along with the answers (I understand it isn't 1 to 1) with my own code: Retrieving data from database in WPF Desktop application but it didn't work in my case.我早些时候发现了这个,并尝试使用我自己的代码跟随答案(我知道它不是一对一): 从 WPF 桌面应用程序中的数据库检索数据,但它在我的情况下不起作用。

I am creating a simple recipe app where a user can create a recipe (eg, put in the title, steps, things they need, etc.) and on the home screen, they can see a link to the recipe that was saved, which would take them to the Recipe Screen to be displayed if clicked.我正在创建一个简单的食谱应用程序,用户可以在其中创建食谱(例如,输入标题、步骤、他们需要的东西等),然后在主屏幕上,他们可以看到保存的食谱的链接,这如果单击,会将它们带到要显示的食谱屏幕。 I am using PostgreSQL for my database and I do see the correct information on there after the user would submit all of the necessary info, I just need to retrieve it and put it in a data grid possibly?我正在为我的数据库使用 PostgreSQL,在用户提交所有必要信息后,我确实看到了正确的信息,我只需要检索它并将其放入数据网格可能吗? Unless there is a better way other than a data grid.除非有比数据网格更好的方法。

Regardless, I plan to have it shown as a list of just the title of the recipe, where a user can click on it and it would load up the page, but that's something I can tackle another time if that is outside of the scope in regards to my question.无论如何,我计划将它显示为仅包含食谱标题的列表,用户可以在其中单击它并加载页面,但如果它不在 scope 中,我可以再解决这个问题关于我的问题。

Here is a visual idea of what I'm trying to accomplish:这是我要完成的工作的直观想法:

在此处输入图像描述

Here is my code for the submit button found in the Create Screen if it helps, however I have no idea what to do in terms of actually retrieving that data and then displaying it on my Home Screen.这是我在创建屏幕中找到的提交按钮的代码,如果它有帮助的话,但是我不知道在实际检索该数据然后在我的主屏幕上显示它方面该怎么做。

        private static NpgsqlConnection GetConnection()
        {
            return new NpgsqlConnection(@"Server=localhost;Port=5432;User Id=postgres;Password=123;Database=RecipeProj;");
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            Recipe recipe = new Recipe();
            recipe.Title = TitleBox.Text;
            recipe.Step1 = StepBox1.Text;
            recipe.Step2 = StepBox2.Text;
            recipe.Step3 = StepBox3.Text;
            recipe.Step4 = StepBox4.Text;
            recipe.Step5 = StepBox5.Text;
            recipe.Step6 = StepBox6.Text;
            recipe.Ingredients = IngredientBox.Text;
            recipe.Tools = ToolBox.Text;
            recipe.Notes = NoteBox.Text;

            void InsertRecord()
            {
                using (NpgsqlConnection con = GetConnection())
                {
                    string query = @"insert into public.Recipes(Title, Ingredients, Tools, Notes, StepOne, StepTwo, StepThree, StepFour, StepFive, StepSix)
                                    values(@Title, @Ingredients, @Tools, @Notes, @StepOne, @StepTwo, @StepThree, @StepFour, @StepFive, @StepSix)";
                    NpgsqlCommand cmd = new NpgsqlCommand(query, con);
                    cmd.Parameters.AddWithValue("@Title", recipe.Title);
                    cmd.Parameters.AddWithValue("@Ingredients", recipe.Ingredients);
                    cmd.Parameters.AddWithValue("@Tools", recipe.Tools);
                    cmd.Parameters.AddWithValue("@Notes", recipe.Notes);
                    cmd.Parameters.AddWithValue("@StepOne", recipe.Step1);
                    cmd.Parameters.AddWithValue("@StepTwo", recipe.Step2);
                    cmd.Parameters.AddWithValue("@StepThree", recipe.Step3);
                    cmd.Parameters.AddWithValue("@StepFour", recipe.Step4);
                    cmd.Parameters.AddWithValue("@StepFive", recipe.Step5);
                    cmd.Parameters.AddWithValue("@StepSix", recipe.Step6);

                    con.Open();
                    int n = cmd.ExecuteNonQuery();
                    if (n == 1)
                    {
                        MessageBox.Show("Record Inserted");
                        TitleBox.Text = IngredientBox.Text = ToolBox.Text = NoteBox.Text = StepBox1.Text = StepBox2.Text = StepBox3.Text = StepBox4.Text = StepBox5.Text = StepBox6.Text = null;
                    }
                    con.Close();
                }
            }

            InsertRecord();
        }
string query = @"select * from Recipes";
NpgsqlCommand cmd = new NpgsqlCommand(query, con);
con.Open();
var reader = cmd.ExecuteReader();

var recipes = new List<Recipe>();

while(reader.Read()){
      //Recipe is just a POCO that represents an entire
      //row inside your Recipes table.
      var recipe = new Recipe(){
          Title = reader.GetString(reader.GetOrdinal("Title")),
          //So on and so forth.
          //...
      };
      recipes.Add(recipe);
}

con.Close();

You can use this same exact query to fill in a List of titles and a DataGrid that shows all the contents of a recipe.您可以使用这个完全相同的查询来填充标题List和显示菜谱所有内容的DataGrid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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