0

我有两个表(1-N 关系)。

第一的(身份证,姓名,姓氏),

第二(ID、工作、角色、社会)。

在我的应用程序中,我想合并 table1 和 table2(基于绑定两个表的 id)但我想隐藏可能为空的列。

示例:(加入在这种情况下,我不想显示“ruolo/grado”)

我是如何为此编写代码的:

CREATE PROCEDURE spEstraiPbyId
    @Id int 
    as
    begin
    SELECT * from Persone  
    join Lavori on Persone.Id = @Id and Lavori.IdPersona=@Id
    end

PS:我已经在网上看到了几个类似的问题,但没有答案能够满足我的要求,或者我没有正确理解。我希望你能心甘情愿地帮助我。

4

2 回答 2

0

谢谢你们。最佳提示是由@Serg 编写的,因为为此我在客户端使用 DataReader 和 Datatable objs 工作:

 DataTable tbl = new DataTable()    
SqlCommand cmd = new SqlCommand("spEstraiPById", cnn); //See at the end for spEstraiPById
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("Id",txtNickname.Text);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    { //Add columns to the table
                        tbl.Columns.Add("ID");
                        tbl.Columns.Add("Nome");
                        tbl.Columns.Add("Cognome");
                        tbl.Columns.Add("Residenza");
                        tbl.Columns.Add("Professione");
                        tbl.Columns.Add("Ruolo");
                        tbl.Columns.Add("Società");

                        while (rdr.Read())
                        { //from DB to Table
                            DataRow drw = tbl.NewRow();
                            drw["ID"] = rdr["Id"];
                            drw["Nome"] = rdr["Nome"];
                            drw["Cognome"] = rdr["Cognome"];
                            drw["Residenza"] = rdr["Residenza"];
                            drw["Professione"] = rdr["Professione"];
                            drw["Ruolo"] = rdr["Ruolo/Grado"];
                            drw["Società"] = rdr["Società"];
                            tbl.Rows.Add(drw);
                        }
                        foreach (DataRow row in tbl.Rows) //Deleting empty records
                        {
                            for (int col = tbl.Columns.Count - 1; col >= 0; col--)
                            {
                                if (row.IsNull(col))
                                {
                                    tbl.Columns.RemoveAt(col);
                                }
                            }
                            // No need to continue if we removed all the columns
                            if (tbl.Columns.Count == 0)
                                break;
                        }
                    }
                        gw1.DataSource = tbl;
                        gw1.DataBind();
                        cnn.Close();
//=Stored Procedure 
CREATE PROCEDURE spEstraiPbyId
    @Id int 
    as
    begin
    SELECT * from Persone 
    join Lavori on Persone.Id = @Id and Lavori.IdPersona=@Id
    end
于 2019-06-28T09:35:18.950 回答
0

如果我理解正确,你想做这样的事情: http ://sqlfiddle.com/#!18/04141/3

SELECT * from Persone  
join Lavori on Persone.Id = Lavori.IdPersona where Lavori.Job is not null

首先,使用 on 加入键,然后使用不为 null 的 where 进行过滤:)

于 2019-06-27T12:59:05.870 回答