【前文】
机房做到这里也是遇到了很多问题的,但是也正是在这个过程中,当然学会了怎么去解决问题这是很关键的,在查的过程中发现C#版的还是很少的,所以也是走了不少弯路,都能成为经历把。
【正文】
敲注册的时候也是按照“七层”走下来的,下面是我的代码:
IDAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace jfIDAL
{
public interface ICard
{
bool IsCardIDExist(int cardID);
int RegisterCard(jfEntity.Student student);
int RechargeCard(int cardID, decimal cardCash);
}
}
DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using jfIDAL;
namespace jfDAL
{
public class DCard:jfIDAL .ICard
{
#region 判断卡号是否存在
/// <summary>
/// 判断卡号是否存在
/// </summary>
/// <param name="studentinfo">参数为实体</param>
/// <returns>卡号存在返回true,否则返回false</returns>
public bool IsCardIDExist(int cardID)
{
string sql = "SELECT cardID FROM [dbo].[Card] WHERE cardID=@cardID AND type='使用'";
SqlDataReader reader = sqlHelper.ExecuteReader(sqlHelper.connectionString, CommandType.Text, sql, new SqlParameter("@cardID", cardID));
return reader.HasRows;
}
#endregion
#region 注册卡号
/// <summary>
/// 注册上机卡号
/// </summary>
/// <param name="studentinfo">参数为实体</param>
/// <returns>注册失败返回0</returns>
public int RegisterCard(jfEntity.Student student)
{
//向Student表中添加信息
string sql = "INSERT INTO [dbo].[Student] VALUES (@stuNo,@stuName,@stuSex,@class,@garde,@department,@major,@explain,@registerDataTime)";
SqlParameter[] sqlparamter = new SqlParameter[]
{
new SqlParameter("@stuNo", SqlDbType.Int),
new SqlParameter("@stuName", SqlDbType.Char,30),
new SqlParameter("@stuSex", SqlDbType.Char,6),
new SqlParameter("@garde", SqlDbType.Char,10),
new SqlParameter("@department", SqlDbType.Char,30),
new SqlParameter("@major", SqlDbType.Char,30),
new SqlParameter("@class", SqlDbType.Char,10),
new SqlParameter("@registerDataTime",SqlDbType.DateTime),
new SqlParameter("@Explain", SqlDbType.Char,50),
};
sqlparamter[0].Value = student.StudentID;
sqlparamter[1].Value = student.StudentName;
sqlparamter[2].Value = student.StudentSex;
sqlparamter[3].Value = student.Grade;
sqlparamter[4].Value = student.Department;
sqlparamter[5].Value = student.Major;
sqlparamter[6].Value = student.Class;
sqlparamter[7].Value = DateTime.Now;
sqlparamter[8].Value = student.Explain;
// sqlparamter[7].Value = student.Explain;
int result=sqlHelper .ExecuteNonQuery (sqlHelper .connectionString ,CommandType.Text ,sql, sqlparamter);
if (result != 0)//T_Student表信息添加成功
{
//向Card表中添加信息
sql = "INSERT INTO [dbo].[Card] VALUES (@cardID,@Cash,@type,@Status,@registerDataTime,@register)";
sqlparamter = new SqlParameter[]
{
new SqlParameter("@cardID",SqlDbType.Int),
//new SqlParameter("@stuID", SqlDbType.Int),
new SqlParameter("@Cash",SqlDbType.Decimal),
new SqlParameter("@type",SqlDbType.Char,10),
new SqlParameter("@Status",SqlDbType.Char,10),
new SqlParameter ("@register",SqlDbType .Char ,10),
new SqlParameter("@registerDataTime",SqlDbType.DateTime)
};
sqlparamter[0].Value = student.CardID;
//sqlparamter[1].Value = student.StudentID;
sqlparamter[1].Value = student.Cash;
//sqlparamter[2].Value = 0;
sqlparamter[2].Value = student.Type;
sqlparamter[3].Value = student.Status;
sqlparamter[4].Value = student.Register;
sqlparamter[5].Value = DateTime.Now;
result = sqlHelper.ExecuteNonQuery(sqlHelper.connectionString, CommandType.Text, sql, sqlparamter);
}
return result;
}
#endregion
#region RechargeCard 卡号充值
/// <summary>
/// 卡号充值
/// </summary>
/// <param name="cardID">卡号</param>
/// <param name="cash">金额</param>
/// <returns>充值失败返回0</returns>
public int RechargeCard(int cardID, decimal cash)
{
//更新card表中的金额和CardRecharge的值
string sql = "Proc_RechargeCard ";//运用存储过程
SqlParameter[] sqlparamter = new SqlParameter[]
{
new SqlParameter("@cash", SqlDbType.Decimal),
new SqlParameter("@cardID", SqlDbType.Int),
};
sqlparamter[0].Value = cash;
sqlparamter[1].Value = cardID;
int result = sqlHelper.ExecuteNonQuery(sqlHelper.connectionString, CommandType.StoredProcedure, sql, sqlparamter);
return result;
}
#endregion
}
}
BLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using jfIDAL;
namespace jfDAL
{
public class DCard:jfIDAL .ICard
{
#region 判断卡号是否存在
/// <summary>
/// 判断卡号是否存在
/// </summary>
/// <param name="studentinfo">参数为实体</param>
/// <returns>卡号存在返回true,否则返回false</returns>
public bool IsCardIDExist(int cardID)
{
string sql = "SELECT cardID FROM [dbo].[Card] WHERE cardID=@cardID AND type='使用'";
SqlDataReader reader = sqlHelper.ExecuteReader(sqlHelper.connectionString, CommandType.Text, sql, new SqlParameter("@cardID", cardID));
return reader.HasRows;
}
#endregion
#region 注册卡号
/// <summary>
/// 注册上机卡号
/// </summary>
/// <param name="studentinfo">参数为实体</param>
/// <returns>注册失败返回0</returns>
public int RegisterCard(jfEntity.Student student)
{
//向Student表中添加信息
string sql = "INSERT INTO [dbo].[Student] VALUES (@stuNo,@stuName,@stuSex,@class,@garde,@department,@major,@explain,@registerDataTime)";
SqlParameter[] sqlparamter = new SqlParameter[]
{
new SqlParameter("@stuNo", SqlDbType.Int),
new SqlParameter("@stuName", SqlDbType.Char,30),
new SqlParameter("@stuSex", SqlDbType.Char,6),
new SqlParameter("@garde", SqlDbType.Char,10),
new SqlParameter("@department", SqlDbType.Char,30),
new SqlParameter("@major", SqlDbType.Char,30),
new SqlParameter("@class", SqlDbType.Char,10),
new SqlParameter("@registerDataTime",SqlDbType.DateTime),
new SqlParameter("@Explain", SqlDbType.Char,50),
};
sqlparamter[0].Value = student.StudentID;
sqlparamter[1].Value = student.StudentName;
sqlparamter[2].Value = student.StudentSex;
sqlparamter[3].Value = student.Grade;
sqlparamter[4].Value = student.Department;
sqlparamter[5].Value = student.Major;
sqlparamter[6].Value = student.Class;
sqlparamter[7].Value = DateTime.Now;
sqlparamter[8].Value = student.Explain;
// sqlparamter[7].Value = student.Explain;
int result=sqlHelper .ExecuteNonQuery (sqlHelper .connectionString ,CommandType.Text ,sql, sqlparamter);
if (result != 0)//T_Student表信息添加成功
{
//向Card表中添加信息
sql = "INSERT INTO [dbo].[Card] VALUES (@cardID,@Cash,@type,@Status,@registerDataTime,@register)";
sqlparamter = new SqlParameter[]
{
new SqlParameter("@cardID",SqlDbType.Int),
//new SqlParameter("@stuID", SqlDbType.Int),
new SqlParameter("@Cash",SqlDbType.Decimal),
new SqlParameter("@type",SqlDbType.Char,10),
new SqlParameter("@Status",SqlDbType.Char,10),
new SqlParameter ("@register",SqlDbType .Char ,10),
new SqlParameter("@registerDataTime",SqlDbType.DateTime)
};
sqlparamter[0].Value = student.CardID;
//sqlparamter[1].Value = student.StudentID;
sqlparamter[1].Value = student.Cash;
//sqlparamter[2].Value = 0;
sqlparamter[2].Value = student.Type;
sqlparamter[3].Value = student.Status;
sqlparamter[4].Value = student.Register;
sqlparamter[5].Value = DateTime.Now;
result = sqlHelper.ExecuteNonQuery(sqlHelper.connectionString, CommandType.Text, sql, sqlparamter);
}
return result;
}
#endregion
#region RechargeCard 卡号充值
/// <summary>
/// 卡号充值
/// </summary>
/// <param name="cardID">卡号</param>
/// <param name="cash">金额</param>
/// <returns>充值失败返回0</returns>
public int RechargeCard(int cardID, decimal cash)
{
//更新card表中的金额和CardRecharge的值
string sql = "Proc_RechargeCard ";//运用存储过程
SqlParameter[] sqlparamter = new SqlParameter[]
{
new SqlParameter("@cash", SqlDbType.Decimal),
new SqlParameter("@cardID", SqlDbType.Int),
};
sqlparamter[0].Value = cash;
sqlparamter[1].Value = cardID;
int result = sqlHelper.ExecuteNonQuery(sqlHelper.connectionString, CommandType.StoredProcedure, sql, sqlparamter);
return result;
}
#endregion
}
}
Facade
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration ;
using System.Reflection;
namespace jfFactory
{
public class FaLogin
{
private static readonly string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
public static jfIDAL.ILogin CreateUser()
{
//通过反射实例化数据库相关类
string ClassName = StrDB + "." + "DLogin";
return (jfIDAL.ILogin)Assembly.Load(StrDB).CreateInstance(ClassName);
}
}
}
UI
private void button10_Click(object sender, EventArgs e)
{
if (txtcard.Text == "")
{
MessageBox.Show("卡号为空", "温馨提示");
txtcard.Focus();
}
else
{
if (txtname1.Text == "")
{
MessageBox.Show("学号为空", "温馨提示");
txtname1.Focus();
}
else
{
jfEntity.Student student = new jfEntity.Student ();
student.CardID = Convert.ToInt32(txtcard.Text);
student.StudentID = Convert.ToInt32(textBox32.Text);
student.StudentName = txtname1.Text.Trim();
student.StudentSex = cobStudentSex.SelectedIndex.ToString();
student.Grade = txtGrade1.Text.Trim();
student.Department = txtDepartment1.Text.Trim();
student.Class = txtClass1.Text.Trim();
student.Major = txtMajor.Text.Trim();
if (textBox30.Text == "")
{
student.Cash = 0;
}
else
{
student.Type = comboBox19.SelectedIndex.ToString();
student.Status = comboBox17.SelectedIndex.ToString();
jfFacade.FCard fcard = new FCard();
int result = fcard.RegisterCard(student);
if (result == 0)
{
MessageBox.Show("卡号已存在!", "温馨提示");
}
else
{
if (result == 1)
{
MessageBox.Show("注册成功!", "请继续");
}
else
{
MessageBox.Show("用户失败!", "请继续");
}
}
【小结】
每每完成一个窗体,都是一次巨大的成长。