从查询中获取值到 C# 中的标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12243118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:36:04  来源:igfitidea点击:

Take value from query to label in C#

c#sql

提问by Azri Zakaria

How can i take value from query result to label?

如何从查询结果中获取价值到标签?

I have two label, one is labelNameand one more is labelDepartment

我有两个标签,一个是labelName,另一个是labelDepartment

So when i run the query, how can i get value from query result and assign it to label using c#?

因此,当我运行查询时,如何从查询结果中获取值并使用 c# 将其分配给标签?

This is my Sql Command:

这是我的 Sql 命令:

  "SELECT tbl_staff.staffName,tbl_department.department 
    FROM tbl_staff,tbl_logs,tbl_department 
    WHERE tbl_staff.userID = tbl_logs." + listStaff.SelectedValue + " and tbl_staff.idDepartment = tbl_department.idDepartment;"

This is current code in C#

这是 C# 中的当前代码

//Open SQL connection

SqlConnection openCon = new SqlConnection(connString);
openCon.Open();

string SQL = string.Format("SELECT tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs.userID and tbl_staff.idDepartment = tbl_department.idDepartment" + listStaff.SelectedValue + ";");


SqlCommand command = new SqlCommand(SQL);
SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
    labelName.Text = reader["tbl_staff.staffName"];
    labelDepartment.Text = reader["tbl_department.department"];
}

Note : Our record will return one row only.. MS SQL and C#.. thanks for help...;)

注意:我们的记录只会返回一行.. MS SQL 和 C#.. 感谢您的帮助...;)

采纳答案by Gregor Primar

        string name = null;
        string department = null;
        string listStaff = "MylistStaff";

        string sql =  "SELECT tbl_staff.staffName,tbl_department.department " +
            "FROM tbl_staff,tbl_logs,tbl_department " +
            "WHERE tbl_staff.userID = tbl_logs." + listStaff + " and tbl_staff.idDepartment = tbl_department.idDepartment;";
        //change this connection string... visit www.connectionstrings.com
        string connString = "Server=localhost; Database=myDatabaseName; Trusted_Connection=Yes";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand command = new SqlCommand(sql,conn))
            {
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    name = reader[0] as string;
                    department = reader[1] as string;
                    //break for single row or you can continue if you have multiple rows...
                    break;
                }
            }
            conn.Close();
        }

department and listStaff can then easily be applied to label text like:

Department 和 listStaff 然后可以轻松应用于标签文本,例如:

DepartmentLabel.Text = department;

DepartmentLabel.Text = 部门;

回答by seeker

You can read content of the row(s) that query returned using DataReaderclass. It has methods to get single value, or you can iterate for each row. Tell me how many rows youR query returns so I can provide the exact code.

您可以使用DataReader类读取查询返回的行的内容。它具有获取单个值的方法,或者您可以对每一行进行迭代。告诉我您的查询返回了多少行,以便我提供确切的代码。

回答by Rohit

Populate the Dataset with your Query and then retrieve values from Dataset and assign it to your label.

使用您的查询填充数据集,然后从数据集中检索值并将其分配给您的标签。

Lets say your Dataset is DS then:

假设您的数据集是 DS,那么:

labelName.Text=DS.Tables[0]["tbl_staff.staffName"].tostring();
labelDepartment.Text=DS.Tables[0]["tbl_department.department"].tostring();

Hope this helps.

希望这可以帮助。

回答by SaschaW

you need to read the result via a SQLDataReader

您需要通过 SQLDataReader 读取结果

SQLCommand command = new SQLCommand("your sql string here");
SQLDataReader reader = command.executeReader();
while(reader.read())
{
 set your label values here with reader["cloumn"]
}

回答by ssilas777

Check this one - Also set your connection string along with this code

检查这个 - 同时设置您的连接字符串以及此代码

SQLCommand command = new SQLCommand();
command.CommandText = " SELECT tbl_staff.staffName,tbl_department.department 
    FROM tbl_staff,tbl_logs,tbl_department 
    WHERE tbl_staff.userID = tbl_logs." + listStaff.SelectedValue + " and tbl_staff.idDepartment = tbl_department.idDepartment ";

SQLDataReader reader = command.executeReader();

while(reader.read())
{
 labelName.Text = reader.GetString(0);
 labelDepartment.Text = reader.GetString(1);
}

回答by Shree

One way to do:

一种做法:

private void getData()
{
    DataTable dt = new DataTable();
    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand(" SELECT tbl_staff.staffName,tbl_department.department 
FROM tbl_staff,tbl_logs,tbl_department 
WHERE tbl_staff.userID = tbl_logs." + listStaff.SelectedValue + " and tbl_staff.idDepartment = tbl_department.idDepartment", connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

    sqlCmd.Parameters.AddWithValue("@username",user);
    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
           lable.Text = dt.Rows[0]["staffName"].ToString(); //Where "staffName" is ColumnName 

    }
        connection.Close();
}

Suggest to use a stored procedure not in line query to avoid SQL injection attacks.

建议使用非在线查询的存储过程,避免SQL注入攻击。

stored procedure example

存储过程示例

回答by Dinesh Guptha

//Open SQL connection

SqlConnection openCon = new SqlConnection(connString);
openCon.Open();

string SQL = string.Format("SELECT tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs.userID and tbl_staff.idDepartment = tbl_department.idDepartment" + listStaff.SelectedValue + ";");


SqlCommand command = new SqlCommand(SQL);
SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
    labelName.Text = reader["tbl_staff.staffName"].ToString();
    labelDepartment.Text = reader["tbl_department.department"].ToString();
}

Convert Type is what you left in your code....

转换类型是您在代码中留下的内容....

回答by SurajVitekar

Try Changing only This

尝试只改变这个

SqlConnection openCon = new SqlConnection(connString);
openCon.Open();

string SQL = string.Format("SELECT tbl_staff.staffName,tbl_department.department FROM tbl_staff,tbl_logs,tbl_department WHERE tbl_staff.userID = tbl_logs.userID and tbl_staff.idDepartment = tbl_department.idDepartment" + listStaff.SelectedValue + ";");


SqlCommand command = new SqlCommand(SQL);
SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
    labelName.Text = reader["tbl_staff.staffName"].toString();
    labelDepartment.Text = reader["tbl_department.department"].toString();
}`