當前位置:編程學習大全網 - 編程軟體 - 數據庫技術應用 C#編程

數據庫技術應用 C#編程

1、

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;

  • 上一篇:最近給孩子講故事枯竭了,有沒有什麽智能機器人可以幫幫忙的?有嗎?
  • 下一篇:UG裝配工程中如何做嚙合齒輪的簡化圖?
  • copyright 2024編程學習大全網