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

时间:2015-12-18 19:01:20

标签: c# sql sql-server attributes sqldatareader

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

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;

谢谢......

2 个答案:

答案 0 :(得分:1)

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

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");

答案 1 :(得分:0)

这基本上是一种最佳做法:

jwplayer("container").setup({                
                    "width": 848,
                    "height": 477,
                    "type": "mp4",
                    "autostart": auto,
                    "provider": "video",                    
                    "file": VIDEO_PATH,
                    "image": THMB_PATH,
                    "skin": { name: "seven", active: "#ff2b2b", inactive: "#fff", background: "rgba(0,0,0,.5)" },
                    "events": { "onReady": function () { vv.setPlayerStatus("Ready"); } },
                    "modes": [
                      { "type": "html5" },
                      { "type": "flash", src: "/content/js/webcam/new/jwplayer.flash.swf" },
                      { "type": "download" }
                    ]                    
                });