使用 ExecuteNonQuery() 的步骤小结:
1、创建 Connection 对象
2、定义sql 语句
3、创建 Command 对象
4、执行 ExecuteNonQuery() 方法
5、根据返回的结果进行处理
string connStr = "…";
SqlConnection connection = new SqlConnection(connStr);
// 插入记录用的 SQL 语句
string sql = string.Format(
"INSERT INTO Grade (GradeName) VALUES ('{0}')",
txtGradeName.Text);
// 创建 Command 对象
SqlCommand command = new SqlCommand(sql, connection);
// 打开数据库连接
connection.Open();
// 执行插入命令
int result = command.ExecuteNonQuery();
connection.Close(); // 关闭数据库连接