我在ms訪問中創建了數據庫,它有GPA列。 在列表框2中,我需要列出GPA大於2的學生。 我該怎麼做?C#訪問數據庫查詢
command.CommandText = "SELECT * FROM Student WHERE GPA > 2";
while (reader.Read())
{
listBox2.Items.Add............?
}
如果可以的話幫助。
我在ms訪問中創建了數據庫,它有GPA列。 在列表框2中,我需要列出GPA大於2的學生。 我該怎麼做?C#訪問數據庫查詢
command.CommandText = "SELECT * FROM Student WHERE GPA > 2";
while (reader.Read())
{
listBox2.Items.Add............?
}
如果可以的話幫助。
我不知道這只是你發佈的僞代碼。但是,如果沒有,您正在閱讀DataReader
,該文件或者未初始化,或者未使用正確的Command
-sql,因爲您在前一行設置了CommandText
。
command.CommandText = "SELECT * FROM Student WHERE GPA > 2";
using(var reader = command.ExecuteReader())
{
while (reader.Read())
{
// assuming that there's a column with name: StudentName
listBox2.Items.Add(reader.GetString(reader.GetOrdinal("StudentName")));
}
}
string ConnectionString = "PUT YOU CONNECTION STRING HERE";
con = new SqlConnection(ConnectionString);
con.Open();
string CommandText = "SELECT * FROM Student WHERE GPA > 2";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
rdr = cmd.ExecuteReader();
lbx.Items.Clear();
while (rdr.Read())
{
lbx.Items.Add......
}
把這個放在try catch中並關閉finally塊中的連接 –
有什麼不對的,你有代碼嗎?除了你永遠不會執行命令來獲取讀者.... – PhoenixReborn
什麼讓你不搜索SO或Google來尋找其中一個答覆simliar *如何以列表視圖/列表顯示來自Access/Excel/SQL的數據盒子/控制檯/任何*問題? –