0

我有 2 张桌子:

  • Document(列DocNo, Doctype, DocTitle
  • Registration(列RegNo, DocNo, RedDate, Location:)

为了显示数据,我编写了这个存储过程:

CREATE Proc Show_Data_ByTitle
    @Title nvarchar(20)
AS
    SELECT * FROM Document WHERE DocTitle = @Title;

    SELECT * FROM Registration 
    WHERE DocNo IN (SELECT DocNo FROM Document WHERE DocTitle = @Title);

现在我想在 gridview -asp.net page- 中显示结果

我怎样才能做到这一点?

4

1 回答 1

0

首先,我认为您需要在 SQL 语句中进行内部联接

SELECT * FROM
Document INNER JOIN Registration
ON Document.DocNo=Registration.DocNo
Where Document.DocTitle=@Title

接下来,如果您使用 ADO.NET 来解释数据,您可能需要一个专门的类来存储数据,例如

public class DocumentRegistration
{
    public int DocNo {get; set;}
    public string DocType {get; set;}
    public string DocTitle {get; set;}
    public int RegNo {get; set;}
    public DateTime RedDate {get; set;}
    public string Location {get; set;}
}

接下来,您可以使用 ADO.NET 将数据放入您的类中

List<DocumentRegistration> list= new List<DocumentRegistration>();
SqlConnection conn = new SqlConnection();
conn.ConnectionString="YourConnectionString"
conn.Open();
SqlCommand cmd = new SqlCommand("Show_Data_ByTitle", conn);
SqlCommand cmd = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("Title", "yourtitle"));

SqlReader reader = cmd.ExecuteReader();
while(reader.Read())
{
    DocumentRegistration dr=new DocumentRegistration
    {
        DocNo =  Convert.ToInt32(reader["DocId"]);
        DocType =  Convert.ToString(reader["DocType"]);
        DocTitle = Convert.ToString(reader["DocTitle"]);
        RedDate = Convert.ToDateTime(reader["RedDate"]);
    }

    list.Add(dr);
}

//put this code where you want to load the list like PageLoad()
GridView1.DataSource=list;
GridView1.Databind();

希望这可以帮助

于 2013-11-03T13:11:06.320 回答