获取数据表中特定用户的特定列 [英] Get specific column of specific user in datatable

查看:91
本文介绍了获取数据表中特定用户的特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,在数据表中,我有3列:USERNAME,PASSWORD和Money。



我想在我的设备中进行特定设置程序来标记用户所拥有的金额,并且对于每个用户,该程序还会显示其他内容,具体取决于当前登录的用户,是否可以,如果可以,我该怎么做?



谢谢。



解决方案

您可以通过编写一些代码来在标签中放入金额。这用于MSSQL数据库连接(在您的情况下)。
下面是一个示例:

  using System.Data.SqlClient; //将其添加到using语句中代码的顶部。 

使用(SqlConnection conn =新的SqlConnection(@数据源=(LocalDB)\MSSQLLocalDB; AttachDbFilename = + Application.StartupPath + @ \Database1.mdf;集成安全性= True))
{
conn.Open();
使用(SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText =在用户名= @用户名的情况下从用户中选择资金;

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

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

label1.Text = reader.GetValue(0).ToString(); //阅读器返回一个对象,您必须将其转换为您的类型。
// GetValue(选定的列号)
}
conn.Close(); //此行是可选的。 using()语句结束时,连接将自动关闭。
}

您可以在Form Load事件中添加此代码。(如果正在工作



第二种方法是使用Dataset并将标签绑定到它。这也很强大,您不需要了解太多sql或如何编写代码,它在一开始就比较复杂,但是更容易并且节省时间。您可以将其应用于任何表单元素(按钮,datagridviews,combobox,文本框等)。



首先,转到标签属性并找到 DataBindings。单击高级。只需单击下一步,直到看到连接到数据库选项。如果您已经使用Visual Studio将您的数据库连接到数据库,它将出现在该组合框中,否则请单击新连接(我想您在基于服务的数据库中工作)。单击下一步并完成。在您将标签绑定到数据库之后(它将在表单加载事件中创建一个生成的代码)。如果表中只有一个记录(一个用户),它将仅显示一个值,但是如果要显示特定用户,则可以在表单加载事件中的其他(已过滤的)事件中更改填充生成的方法。 SQL子句)。您可以在表单设计器底部的添加的数据集中更改该填充方法。单击旁边的小箭头,然后选择在设计器中编辑选项。单击表适配器部分,然后右键单击其功能(在本例中为Fill()方法),然后单击配置。在这里,您可以更改sql语句,并在最后放置WHERE子句。(例如,其中Username =?)?表示一些变量。传递在表单加载事件中创建的函数后,该数据集事物旁边的用户名。做完了如果要使用Win Forms和sql数据库,我建议您学习如何使用数据集,绑定和TableAdapter。希望能帮助到你。
我的解释的屏幕截图:













!!!! [更新] !!!!
这是我在google驱动器上的示例程序:





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.

解决方案

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:

!!!! [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.

这篇关于获取数据表中特定用户的特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆