【问题标题】:how to concatenate different column of sql and showing it in datagridview c#如何连接不同的sql列并在datagridview c#中显示它
【发布时间】:2014-12-21 17:26:26
【问题描述】:

我有SQL 表,它有几列我想连接一些列并编辑它们,然后在datagrid 视图中显示它们我在这里遇到一个问题,我正在从sql 检索数据但是我不能将它们保存在变量中,也不能操作数据,然后将其插入 datagrid ,我想将数据放入 datagrid 中,然后操作隐藏的 datagridview 数据,然后插入新变量在新的datagrid 视图中,但是有没有一种更简单的方法来做到这一点,就像php 中的那样,我们可以在数组中获取返回的数据,然后使用变量,或者我应该像我描述的那样去。

【问题讨论】:

  • 写一个合适的选择语句来完成所有这些工作怎么样?

标签: c# sql winforms datagridview


【解决方案1】:

可以使用sql语句来连接列

select column1+' '+column2 from table       - in SQl server 
select column1|| ' '|| column2 from table   - in Oracle 
select concat(column1, concat(' ', column2)) from table    -in Mysql

并使用数据源绑定绑定到 datagridview

【讨论】:

    【解决方案2】:
    SqlConnection conn = new SqlConnection("YourConnectionString");
    
    conn.Open();
    
    SqlCommand cmd = new SqlCommand("Query for fetching your data", conn);
    //cmd.Parameters.AddWithValue("@P1", p1Value); if the query need parameters to prevent sql injection.
    
    DataSet resultDst = new DataSet();
    
    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
    {
        dapter.Fill(resultDst, "TableName");
    }
    
    conn.Close();
    
    foreach(DataRow row in resultDst.Tables[0].Rows)
    {
       //manuipulate the data if needed
       //row["ColumnName"] = some value;
    }
    
    
    //if this is winforms you need only datagrid dataSource property to be set, if you are using asp.net //you need to databind after that.
    
    dataGridView1.DataSource = resultDst.Tables[0];
    

    根据你的解释,你需要这样的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多