存储过程-连接查询可以在Sql中工作,但无法在Datagridview c#中加载
本文关键字:Datagridview 加载 工作 查询 存储过程 Sql 连接 | 更新日期: 2023-09-27 18:13:23
它在SQL-server连接查询中工作。在c#中,我正在调用数据网格视图,但输出不像预期的那样??这是一项艰苦的工作,所以离开
第1个表
BookId | BooKName | DateIssue| ReturnDate | PersonID
| | | |
1 | c# |2007-07-07|2007-07-07 | 105
Person第二表
PersonID | PersonName | PersonType
| |
1 | John | student
现在,我在SQL server中创建了存储过程…查询:
ALTER PROCEDURE [database]. [joinIssuetable]
as
select * from table Issue
select * from table Person
Select Book ID,Book Name,Date Issue,Return Date,Person Name
from tableIssue
join tablePerson
on tableIssue.Person ID = tablePerson.PersonID
===========================================================================在c#中调用Join查询,但数据网格视图没有显示预期的输出。
public void Stored Procedure()
{
string w = ConfigurationManager.ConnectionStrings["LMS"].ConnectionString;
SqlConnection conn = new SqlConnection(w);
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("joinIssuetable", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
conn.Open();
sda.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
}
您不需要在StoredProcedure代码中添加Select * from ...
。只需编辑您的过程,使其只有一个结果集:
ALTER PROCEDURE [database]. [joinIssuetable]
AS
SELECT BookID, BookName, DateIssue, ReturnDate, Person Name
FROM tableIssue
JOIN tablePerson
ON tableIssue.PersonID = tablePerson.PersonID