【问题标题】:c# display table from sql server on winformc#在winform上显示来自sql server的表
【发布时间】:2011-05-04 13:58:22
【问题描述】:

我正在使用 c# 连接到 sql server。如何在 winform 上显示以下查询的结果?我想在控件中显示此数据集。我相信它应该是一个数据图表,但这对我来说并不重要。

// Initialize a connection string    
string myConnectionString = "Provider=SQLOLEDB;Data Source=hermes;" +
   "Initial Catalog=qcvaluestest;Integrated Security=SSPI;";

// Define the database query    
string mySelectQuery = "select top 500 name, finalconc " + 
   "from qvalues where rowid between 0 and 25000";

在 winform 上显示此查询结果的最佳方式是什么?

【问题讨论】:

    标签: c# .net sql sql-server-2008 select


    【解决方案1】:

    在表单上放置一个 DataGridView,并使用此代码填充它

    using(var connection = new SqlConnection(myConnectionString))
    using(var adapter = new SqlDataAdapter(mySelectQuery, connection))
    {
       var table = new DataTable();
       adapter.Fill(table);
       this.dataGridView.DataSource = table;
    }
    

    【讨论】:

    • 我应该导入哪个命名空间以便使用 sqldataadpter
    • SqlClient 是您要导入的命名空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多