我在GridView
从 SQL Server 提取信息时,在我的 . 我正在使用存储过程并使用一些 C# 来设置参数,然后使用 将SqlDataSource
存储过程中的 sql 数据放入GridView
. 我也看过一些其他的问题,就像我的一样,但是 SQL 脚本与我的不同。我也知道我的 C# 代码工作得很好,但我也会发布,
这是代码:
SQL:
SELECT
people.name, comage.age, grad.date
FROM
people
INNER JOIN
(SELECT age, MAX(term)[term]
FROM comage GROUP BY date) comage ON people.term = comage.term
INNER JOIN
(SELECT date, MAX(term)[term]
FROM grad GROUP BY date) grad ON people.term = grad.term
WHERE
people.ID = @ID
ORDER BY
LastName
ASP.NET:
<asp:GridView ID="grid" runat="server"></GridView>
C#代码:
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "prcedureName";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = null;
param = cmd.Parameters.Add("@ID",System.Data.SqlDataType.NChar,255);
param.Direction = System.Data.ParameterDirection.Input;
param.value = in; //in is a string of some value
cmd.Connection = con; //con is a SqlConnection of some sort
SqlDataAdapter adp = new SqlDataAdapter(cmd);
SqlCommandBuilder command = new SqlCommandBuilder(adp);
DataTable data = new DataTable();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;
adp.Fill(data);
grid.DataSource = data;
grid.AutoGenerateColumns = true;
grid.DataBind();
以下是表格:
人表:
--------------------
|name | ID | term |
--------------------
|jeff | 0 | A |
|Jake | 1 | B |
--------------------
图像表:
--------------------
|Age | Term |
--------------------
|23 | A |
|25 | B |
--------------------
毕业表:
--------------------
| Date | Term |
--------------------
| DEC | A |
| SUM | B |
--------------------