Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

c# - Convert SqlDataSource to DataTable and DataView

I have the following SqlDataSource and I want to convert it to DataView and read a column from it:

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

This SqlDataSource lets the user enter the number of the Quiz, and it will retrieve the total number of participants in that quiz. I want to convert this SqlDataSource to a DataTable and read a column from it.

So how to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let me say you have an SqlDataSource named as SqlDataSource1

DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView view = (DataView)SqlDataSource1.Select(args);
DataTable dt = view.ToTable();

Now you can read a column easily from this dt(DataTable) e.g.

int columnNumber = 0;
for(int i=0;i<dt.Rows.Count;i++)
{
    MessageBox.Show(dt.Rows[i][columnNumber].ToString());
}

Reference


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...