图4.3.11
图4.3.12
14
第五章 主要功能模块代码
5.1 公共类代码设计
namespace MyHelper {
class SqlHelper {
public static readonly string connstr =
ConfigurationManager.ConnectionStrings[\].ConnectionString;
public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters) {
using (SqlConnection conn = new SqlConnection(connstr)) {
conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) {
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } }
public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters) {
using (SqlConnection conn = new SqlConnection(connstr)) {
conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) {
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } }
15
public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters) {
using (SqlConnection conn = new SqlConnection(connstr)) {
conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) {
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters);
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) {
DataTable dt = new DataTable(); adapter.Fill(dt); return dt; } } } }
public static SqlDataReader ExecuteDataReader(string cmdText, params SqlParameter[] parameters) {
SqlConnection conn = new SqlConnection(connstr); conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) {
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteReader(CommandBehavior.CloseConnection); } } } }
5.2 登录界面代码设计
private void login_Click(object sender, EventArgs e) {//用户名及密码组合判断
if (username.Text.ToString().Trim() != \&& password.Text.ToString().Trim() != \
16
{
if (txtUserName.Text == \ || txtUserName.Text == \强强\) {
if (txtPsw.Text == \) {
MessageBox.Show(\登录成功!\, \提示?\, MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; } else {
MessageBox.Show(\密码错误,您还有\ + i + \次机会...\); i--; } } else {
MessageBox.Show(\亲,没有此用户名\); }
}
5.3 查询模块代码设计
QueryFrm query = new QueryFrm();
if (query.ShowDialog() == DialogResult.OK) {
DataTable dt = SqlHelper.ExecuteDataTable(\编号',Name as '姓名',Sex as '性别',English as '英语',Chinese as '语文', Math as '数学' from T_Student where name = @name\,new SqlParameter(\, query.Name));
if (dt.Rows.Count>=1) {
dataGridView1.DataSource = dt; } else {
MessageBox.Show(\没有找到您所查询的结果,请重新查询\,\提
17
示\,MessageBoxButtons.OK,MessageBoxIcon.Information); } }
5.4 添加模块代码设计
if (txtName.Text.Trim() == \ ||
txtChi.Text.Trim() == \ || txtEng.Text.Trim() == \ || txtMath.Text.Trim() == \ ) {
MessageBox.Show(\请将信息填写完整\, \警告?\,
MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else {
if (rbMale.Checked) {
SqlHelper.ExecuteNonQuery(\T_Student(name,sex,english,math,chinese) values(@name,@sex,@Eng,@Math,@Chi)\, new SqlParameter(\, txtName.Text),
new SqlParameter(\, Convert.ToInt32(txtEng.Text)),
new SqlParameter(\,\男D\), new SqlParameter(\, Convert.ToInt32(txtChi.Text)),
new SqlParameter(\, Convert.ToInt32(txtMath.Text)) ); } else {
SqlHelper.ExecuteNonQuery(\T_Student(name,sex,english,math,chinese) values(@name,@sex,@Eng,@Math,@Chi)\, new SqlParameter(\, txtName.Text),
new SqlParameter(\, txtEng.Text),
new SqlParameter(\, \女?\), new SqlParameter(\, txtChi.Text),
18
相关推荐: