C#DataReader 實(shí)例
(1)新建項(xiàng)目(WINDOWS應(yīng)用程序)。
(2)雙擊Form1,進(jìn)入Load事件。
(3)在Form1.cs中添加命名空間using System.Data.OleDb。
(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+=“學(xué)號(hào):”+reader[0].ToString( )+” “;
message+=“姓名:”+reader[“sName”].ToString( )+” “;
message+=“班級(jí):”+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);}
點(diǎn)擊加載更多評(píng)論>>