Command
Command对象的属性
CommandText:需要封装的sql语句或存储过程名称。
Connection:Command对象使用的数据库连接对象。
Command对象的方法:
ExecuteNonQuery() 执行增、删、改操作。
ExecuteScalar() 返回单一结果的查询。
ExecuteReader() 返回只读数据表的查询。
ExecuteNonQuery()
该方法执行insert、Update、Delete类型的语句。
执行后返回受影响的行数,一般是大于0的整数,等于0说明没有影响,-1表示出现错误。
增、的实体举例:
//编写连接字符串
string connString = "Server=.;DataBase=StudentManageDB;Uid=sa;Pwd=aaa999a9";
//创建连接对象
SqlConnection conn = new SqlConnection(connString);
//组合SQL语句
string sql = "insert into Students(StudentName,Gender,Birthday,";
sql += "StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId)";
sql += "values ('{0}','{1}','{2}',{3},{4},'{5}','{6}',{7})";
sql = string.Format(sql, "李小万", "女", "1990-10-10",
125478965241259639, 28, "020-1236985", "广东深圳", 3);
SqlCommand cmd = new SqlCommand(sql, conn);