从数据库 (SQL) 中获取属性并将它们放在 C# 中的 label.Text 中

Take attributes from database (SQL) and place them in label.Text in C#

我想知道如何将特定属性从我的数据库放置到标签中,例如我想读取一个人的姓名并将其放置到表单标签中

SqlCommandSqlDataReader - 为此我需要使用哪一个?

我试过 SqlDataReader 但它不会给我任何结果或引发 SqlException 错误。

我试过这样的事情: 我的密码是土耳其语

SqlCommand cmd = new SqlCommand();
SqlParameter param = new SqlParameter();
cmd.CommandText = "SELECT Ad FROM OGRENCILER WHERE OgrenciKartID=@id";
param.SqlDbType = SqlDbType.Int;
param.ParameterName = "@id";
param.Value = _kimlik;
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
if (connection.State == ConnectionState.Closed)
    connection.Open();

var adId = cmd.ExecuteScalar().ToString();
connection.Close();

adLabel.Text = adId;

谢谢...

您可以从以下内容推断出答案,它展示了如何从 SQL 服务器数据库中读取数据,将其存储在通用字符串列表中,然后将列表中的第一个值分配给标签的文本 属性:

private List<String> _duckbillsList;
. . .
private void LoadduckbillstringList()
{
    if (null == _duckbillsList)
    {
        _duckbillsList = new List<string>();
    }
    using (SqlConnection con = new SqlConnection(PlatypusConstsAndUtils.CPSConnStr))
    {
        using (SqlCommand cmd = new SqlCommand(PlatypusConstsAndUtils.SelectPlatypusIdOnlyQuery, con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                _duckbillsList = dt.AsEnumerable()
                   .Select(p => p.Field<string>("platypusId"))
                   .ToList();
            }
        }
    }
}

labelFirstPlatypus.Text = _duckbillsList[0].ToString();

更新

好的,这个可能更容易理解和适应:

public static string GetPlatypusNameForlatypusId(string platypusId)
{
    SqlConnection sqlConn = new SqlConnection(CPSConnStr);
    SqlCommand cmd = new SqlCommand();
    SqlParameter param = new SqlParameter();

    cmd.CommandText = "SELECT PNAME FROM DUCKBILLS WHERE PLATYPUSID = @PLATYPUSID";
    param.SqlDbType = SqlDbType.VarChar;
    param.ParameterName = "@PLATYPUSID";
    param.Value = platypusId;
    cmd.CommandType = CommandType.Text;
    cmd.Connection = sqlConn;

    sqlConn.Open();

    var platypusName = cmd.ExecuteScalar().ToString();

    sqlConn.Close();
    return platypusName;
}

labelPlatypusName.Text = GetPlatypusNameForPlatypusId("4F");

这基本上是最佳实践:

//create connection, with a connection string
using(var conn = new SqlConnection(connectionString)) {
    try {
        conn.Open();
    }
    catch(Exception ex) {
        //Handle exception
    }

    if(conn.State == System.Data.ConnectionState.Open) {
        var commandText = "SELECT Ad FROM OGRENCILER WHERE OgrenciKartID=@id";
        //"using" helps in automatically disposing of the object once it's done
        using(var comm = new SqlCommand(commandText, conn)) {
            //one of the proper ways to add a parameter, which also sets the value
            comm.Parameters.Add("@id", SqlDbType.Int).Value = _kimilik;
            //the rest is correct, assuming the connection can be established and this is valid SQL (correct DB structure)

            object result = null;
            try {
                result = comm.ExecuteScalar();
            }
            catch(Exception ex) {
                //handle exception
            }
            if(result != null) {
                var adId = result.ToString();
                adLabel.Text = adId;
            }
        }
        conn.Close();
    }
}