Get specific column of specific user in datatable

心已入冬 提交于 2020-06-17 12:54:43

问题


I have a data table, in the data table, I have 3 columns: USERNAME, PASSWORD, and Money.

I want to make a specific setting in my program to make a label the amount of money the user has, and for each user, it will show something else depending on who is logged on at the moment, is it possible and if yes how can I do it?

Thanks in advance.

Database layout


回答1:


You can put the amount of money in your label by writing some code. This is used for MSSQL Database connections(in your case). Here is an example:

using System.Data.SqlClient;//Add this in the using statements at the top of your code.

using (SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename =" +Application.StartupPath+ @"\Database1.mdf; Integrated Security = True"))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = "SELECT Money FROM Users WHERE Username = @username";

                cmd.Parameters.AddWithValue("@username", "User1");

                var reader = cmd.ExecuteReader();
                reader.Read();

                label1.Text = reader.GetValue(0).ToString(); //reader returns an object, you have to convert it in your type.
                //GetValue(selected column number)
            }
            conn.Close();//This line is optional. The connection closes automatically when the using() statement ends.
        }

You can add this code in your Form Load event.(If you are working with Win Forms).

The second method is to use Dataset with binding the label to it. This is also powerful and you do not need to know too much sql or how to code, it is more complicated at the beginning but it`s easier and saves time. You can apply it to any of your form elements(buttons, datagridviews,combobox,textbox,etc).

First, go to your label properties and find "DataBindings". Click on advanced. Just click next until you see the connect to database option. If your are connected already with visual studio to your database it will appear in that combobox, otherwise click new connection(I suppose you worked in service based database).Click next and finish. After you have to bind the label to the database(it will create a generated code in your Form Load Event). If you have only one record(one user) in the table it will show only one value, but if you want to show a specific user, you can change that "Fill" generated method in your Form Load Event in other(filtered one with WHERE SQL Clause). You can change that fill method in the Dataset Added in the bottom of your designer of the form. Click on that little arrow near it and choose "edit in designer" option. Click on the table adapter section and right click on his function(in this case Fill() method) and click configure. Here you can change the sql statement and put a WHERE clause in the end.(ex Where Username = ?) The "?" means some variable. After pass in the function created in the form load event your user`s username next to that dataset thing. Done. If you want to work with Win Forms and sql databases I advise you to learn how to use The Datasets, Bindings and TableAdapters. Hope it helps. Screenshots of my explanations:

Step1Step2Step3Step4Step5Step6Step7Step8Step9

!!!! [UPDATE] !!!! Here is my example program on google drive: Link. On the right side you can open my service based database(in the project files(Database1)).I'll attach some useful screenshots for creating addition functions in a dataset's table adaper. Also, you have the second method commented in the Form1 load event.



来源:https://stackoverflow.com/questions/60176742/get-specific-column-of-specific-user-in-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!