Windows 窗体绑定到来自 SQL 查询的测试框

本文关键字:查询 测试 SQL 窗体 绑定 Windows | 更新日期: 2023-09-27 18:35:47

我确定这很简单,但似乎无法让它工作。

我有以下代码,当前用于使用 SQL 查询返回的值更新 Windows 窗体组合框。

我现在需要做同样的事情,即从 SQL 查询返回服务器名称,但我需要填充一个标签,而不是填充组合框。

//Create new sql connection
                SqlConnection db = new SqlConnection();
                //to refresh connection string each time else it will use previous connection string
                ConfigurationManager.RefreshSection("connectionStrings");
                db.ConnectionString = ConfigurationManager.ConnectionStrings["ToolboxDatabase"].ToString();
//Check connection string has update has worked
                SqlDataAdapter da = new SqlDataAdapter("select @@Server AS ServerName", db);
                DataTable dt = new DataTable();
                da.Fill(dt);
                cmbTestValue.DataSource = dt;
                cmbTestValue.DisplayMember = "ServerName";

请帮忙

Windows 窗体绑定到来自 SQL 查询的测试框

试试这个:

SqlDataAdapter da = new SqlDataAdapter("select @@Server AS ServerName", db);
DataTable dt = new DataTable();
Label1.Text = dt.Rows[0]["ServerName"].ToString();