位置:首頁 > 軟件操作教程 > 編程開發(fā) > C# > 問題詳情

C#DataReader 實例

提問人:劉冬梅發(fā)布時間:2020-10-13

(1)新建項目(WINDOWS應用程序)。

(2)雙擊Form1,進入Load事件。

(3)在Form1.cs中添加命名空間using?。樱螅簦澹恚模幔簦幔希欤澹模?。

(4)在Load事件中添加代碼:

string message=“ “;

OleDbConnection conn=new OleDbConnection() ;

conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=“+

                                 @"Data\student.mdb";

string selectQuery=“select ID,sName,sGrade,sSex from student”;

OleDbCommand cmd=new OleDbCommand(selectQuery,conn);

conn.Open( );

OleDbDataReader reader=com.ExecuteReader(CommandBehavior.CloseConnection);

while(reader.read( ))

{ message+=“學號:”+reader[0].ToString( )+” “;

  message+=“姓名:”+reader[“sName”].ToString( )+” “;

 message+=“班級:”+reader.GetString(2)+” “;

message+=“性別:“+reader.GetString(3)+” “;

message+=“\n”;

}

reader.Close( );

if(conn.State==ConnectionState.Closed) message+=“數(shù)據(jù)連接已經(jīng)關(guān)閉”;

MessageBox.Show(message);}

繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(0)
返回頂部