我有SQL
表,其中有几列我要连接一些列并编辑它们然后在datagrid
视图中显示它我在这里遇到问题我是在检索数据来自sql
,但我无法将它们保存在变量中,操纵数据然后将其插入datagrid
,我想把数据放在datagrid
中,然后操纵隐藏{ {1}}数据然后在新的datagridview
视图中插入新变量,但有一种更简单的方法可以像datagrid
中那样获取数组中的返回数据然后使用变量,或者我应该像我描述的那样。
答案 0 :(得分: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
答案 1 :(得分:0)
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];
从你的exlanation你需要这样的东西。