我有以下SqlDataSource
,我想将其转换为DataView
并从中读取一列:
SELECT
dbo.Divisions.DivisionShortcut,
COUNT(DISTINCT dbo.UserQuiz.Username) AS [Number of Participants]
FROM
dbo.Divisions
INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode
INNER JOIN
dbo.UserQuiz ON dbo.employee.Username = dbo.UserQuiz.Username
INNER JOIN
dbo.Quiz ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID
WHERE
(dbo.Quiz.QuizID = @QuizID)
GROUP BY
dbo.Divisions.DivisionShortcut
该
SqlDataSource
允许用户输入测验的编号,它将检索该测验中的参与者总数。我想将此SqlDataSource
转换为DataTable
并从中读取一列。那么该怎么做?
最佳答案
让我说你有一个名为SqlDataSource1的SqlDataSource
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView view = (DataView)SqlDataSource1.Select(args);
DataTable dt = view.ToTable();
现在您可以轻松地从此dt(DataTable)中读取一列,例如
int columnNumber = 0;
for(int i=0;i<dt.Rows.Count;i++)
{
MessageBox.Show(dt.Rows[i][columnNumber].ToString());
}
Reference
关于c# - 将SqlDataSource转换为DataTable和DataView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9452763/