Windows 表单绑定到来自 SQL 查询的测试框
Windows forms binding to test box from SQL query
我确定这非常简单,但似乎无法正常工作。
我有以下代码,当前用于更新 Windows 表单组合框,其值为 return 来自 SQL 查询的值。
我现在需要做同样的事情,即 return 来自 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";
请帮忙
试试这个:
SqlDataAdapter da = new SqlDataAdapter("select @@Server AS ServerName", db);
DataTable dt = new DataTable();
Label1.Text = dt.Rows[0]["ServerName"].ToString();
我确定这非常简单,但似乎无法正常工作。
我有以下代码,当前用于更新 Windows 表单组合框,其值为 return 来自 SQL 查询的值。
我现在需要做同样的事情,即 return 来自 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";
请帮忙
试试这个:
SqlDataAdapter da = new SqlDataAdapter("select @@Server AS ServerName", db);
DataTable dt = new DataTable();
Label1.Text = dt.Rows[0]["ServerName"].ToString();