String sqlstr=String.format("select * from TStudent where ClassId='{0}'",stringclassId);
DataTable dt=new DateTable();
m_connection.open();
SqlCommand comm = new SqlCommand();
comm.CommandText = sqlstr;
comm.Connection = conn;
try
{
using (SqlDataReader dr = comm.ExecuteReader())
{
dt.Load(dr); //將查詢數據保存到datatable裏更方便吧
}
}
catch (Exception ex)
{
Console.Write(ex.message);
}
//另附讀取dt數據的方法:如讀取dt第壹行的Name字段:
String s=dt.rows[0]["Name"].ToString();
2、
String sqlstr=String.format("insert into TStudent values('{0}','{1}','{2}','{3}','{4}')",st.StudentId,st.Name,st.Email,st.ClassId,st.Birthday);
m_connection.open();
SqlCommand comm = new SqlCommand();
comm.CommandText = sqlstr;
comm.Connection = conn;
comm.ExecuteNonQuery();
3、
int studentcount=0;
sqlstr="select count(StudentId) as count from TStudent ";
DataTable dt=new DateTable();
m_connection.open();
SqlCommand comm = new SqlCommand();
comm.CommandText = sqlstr;
comm.Connection = conn;
using (SqlDataReader dr = comm.ExecuteReader())
{
dt.Load(dr);
}
studentcount=Int32.Parse(dt.rows[0]["count"].toString());
return studentcount;