分类: 数据库开发技术
2009-07-14 17:51:23
1. 在命名空间处导入名称(命名空间):using System..Data.SqlClient;
2. 打开数据库:(在大括号中连接数据库)
{
Sqlconnection conn = new Sqlconnection(“server=.;database=studentsytem;Trtsted_Connection=sspi”)(window身份验证);
Conn.Open();
}
4登陆时判断用户名是否存在:
1.进入数据库语句:
SqlCommand cmd = new SqlCommand(“select count (*) from users where username=@userName,conn”);
2.取出相关的列语句:
Cmd.Parameters.Add(“@userName”,sqlDbType.varchar,20).Valus=this.txtUserName.Text;
3. 声明只读器
(1) 只取一条数据语句:
SqldataReader dr = cmd.ExecuteReader();
Int I =0;
While(dr.Read())
{
I = Convert.Toint32(dr[0].ToString());
}
Dr.Close();
(2) 取多条数据语句:
ArrStudentScore =new Arraylist();
SqlDataReader dr =cmd.ExecuteReader();
While (dr.Read())
{
StudentScoreInfo studentScore =new StudentScoreInfo();
StudentScore.StuID =dr[0].ToString();
StudentScore.StuName =dr[1].ToString();
StudentScore.StuClass =dr[2].ToString();
StudentScore.StuSubject =dr[3].ToString();
StudentScore.StuScore =dr[4].ToString();
StudentScore.inserDateTime =dr[5].ToString();
ArrStudentScore.Add (studentscore);//放入数组
}
Dr.Close();//关闭只读器