一、添加年级信息到表中
/// <summary>
/// 添加年级表
/// INSERT INTO grade VALUES('IT11')
/// </summary>
private static void addGrade(string gname)
{
SqlConnection con = null;
try
{
//1.连接字符串
string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
//2.连接对象
con = new SqlConnection(str);
//3.打开连接
con.Open();
//4.sql语句
//INSERT INTO grade VALUES('IT11')
StringBuilder bulider = new StringBuilder();
bulider.AppendLine("INSERT");
bulider.AppendLine(" INTO ");
bulider.AppendLine(" grade ");
bulider.AppendLine(" VALUES ");
bulider.AppendFormat("('{0}') ", gname);
// Console.WriteLine(bulider.ToString());
//5.命名对象
SqlCommand cmd = new SqlCommand(bulider.ToString(), con);
//6.执行sql语句 返回Int
int count = cmd.ExecuteNonQuery();
// Console.WriteLine(count);
if (count > 0)
{
Console.WriteLine("OK!");
}
else
{
Console.WriteLine("no!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
con.Close();
}
}
二、修改学生的信息
/// <summary>
/// 修改学生表
/// UPDATE stu SET BornDate='1992-1-1',Phone='80011011' WHERE StudentName='白燕'
/// </summary>
private static void updateStudent(string date,string tel,string name)
{
SqlConnection con = null;
try
{
//1.连接字符串
string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
//2.连接对象
con = new SqlConnection(str);
//3.打开连接
con.Open();
//4.sql语句
//UPDATE stu SET BornDate='1992-1-1',Phone='80011011' WHERE StudentName='白燕'
StringBuilder bulider = new StringBuilder();
bulider.AppendLine("UPDATE");
bulider.AppendLine(" stu ");
bulider.AppendLine(" SET ");
bulider.AppendFormat(" BornDate='{0}',Phone='{1}' ",date,tel);
bulider.AppendLine(" WHERE ");
bulider.AppendFormat("StudentName='{0}' ", name);
// Console.WriteLine(bulider.ToString());
//5.命名对象
SqlCommand cmd = new SqlCommand(bulider.ToString(), con);
//6.执行sql语句 返回Int
int count = cmd.ExecuteNonQuery();
// Console.WriteLine(count);
if (count > 0)
{
Console.WriteLine("OK!");
}
else
{
Console.WriteLine("no!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
con.Close();
}
}
三、删除学生信息
/// <summary>
/// 删除学生表
/// delete stu WHERE StudentName='白燕'
/// </summary>
private static void delStudent(string name)
{
SqlConnection con = null;
try
{
//1.连接字符串
string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
//2.连接对象
con = new SqlConnection(str);
//3.打开连接
con.Open();
//4.sql语句
// delete stu WHERE StudentName='白燕'
StringBuilder bulider = new StringBuilder();
bulider.AppendLine("delete");
bulider.AppendLine(" stu ");
bulider.AppendLine(" WHERE ");
bulider.AppendFormat("StudentName='{0}' ", name);
// Console.WriteLine(bulider.ToString());
//5.命名对象
SqlCommand cmd = new SqlCommand(bulider.ToString(), con);
//6.执行sql语句 返回Int
int count = cmd.ExecuteNonQuery();
// Console.WriteLine(count);
if (count > 0)
{
Console.WriteLine("OK!");
}
else
{
Console.WriteLine("no!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
con.Close();
}
}
四、总结
操作添加,删除,修改数据,其实就是用SQLCommand命令对象中的ExecuteNonQuery()来执行,返回是一个整数型的数据。