N-Tier总结
1. 连接字符串:Data Source=.; Initial Catalog=数据库名; User ID=用户名; Password=密码;
2. 程序集:程序集由描述它的程序集清单(包括版本号,程序集名称等)、类型元数据、MSIL代码和资源组成。程序集包括两类:可执行文件,既.exe文件,类库文件.dll文件。
3. 程序集、解决方案、项目、和命名空间的关系如下:一个解决方案由一个或者多个项目组成,这些项目可以是windows应用程序、类库等。一个程序集可以包含多个命名空间,程序集默认空间名称就是程序集的名称。命名空间是组织C#程序的一种逻辑结构,一个命名空间可以有多个类。
4. 分层架构的主要优势如下
u 代码服用
u 分离开发人员的关注
u 无损替换
u 降低系统之间的依赖
5. 常用的异常处理语句包括try-catch、try-finally、try-catch-finally只有在catch里可以throw。
6、使用下一层的步骤。
u 在上一层添加引用
u 在上一层加下一层命名空间
7、XML的典型应用:配置文件
Ø 在表示层添加连接字符串
Ø 在数据库访问层添加引用
Ø 在要使用的类名添加命名空间System.Configuration
8、readonly和Const的对比
Ø Const能修饰类中的字段或者局部变量,readonly只能修饰类的字段
Ø 对于引用类型,除了string外,一般不声明const如果非要声明const初始值为null
Ø Const修饰的常量必须在声明时进行赋值,而readonly常量可以在程序运行时赋值。
9、如何使用using语句释放对象占用资源,被using语句限定的对象在使用完被及时释放,这个对象的类实现了IDisposable接口
10使用sqlParameter传递Sql语句的参数
根据参数创建sqlParameter对象,然后添加到sqlCommand.parameters对象中例子:事务,带输出参数的存储过程,带返回值的存储过程
在ADO.NET中使用事务需要引入System.Data.SqlClient命名空间通过
SqlConnection的BeginTransaction()方法创建SqlTransation对象
publicint deleteStudent(int stuNo)
{
SqlTransaction trans = null;
SqlConnection con= newSqlConnection(connString);
try
{
con.Open();
trans = con.BeginTransaction();
string sql = "delete from result whereStudentNo=@stuNo";
SqlCommand cmd = newSqlCommand();
cmd.CommandText = sql;
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
SqlParameter para = newSqlParameter("@stuNo",SqlDbType.Int);
para.Value=stuNo;
cmd.Parameters.Add(para);
//第二条SQL语句
cmd.CommandText = "delete from student where StudentNo=@stuNo";
cmd.Transaction = trans;
cmd.ExecuteNonQuery();
trans.Commit();
return 1;
}
catch (Exception)
{
trans.Rollback();
return 0;
}
}
----------------------------------------------------------------------------------------------------------------------------------
publicstaticint DeleteStudentInfo(int stuNo)
{
//创建SqlConnection对象
SqlConnection conn = newSqlConnection(connString);
try
{
//初始化返回值
int iRet = -1;
// 创建Command命令
SqlCommand cmd = newSqlCommand("sp_delete_student", conn);
cmd.CommandType = CommandType.StoredProcedure;
//添加输入参数
cmd.Parameters.Add("@StudentNo", SqlDbType.Int).Value= stuNo
conn.Open();
iRet = cmd.ExecuteNonQuery();
return iRet;
}
catch (Exception ex)
{
throw ex;
}
finally
{
//关闭数据库连接
conn.Close();
}
}
----------------------------------------------------------------------------------------------
publicboolloginCheck(String name, String pwd)
{
string reValue = "";
try
{
SqlConnection con = new SqlConnection(DBHelper.conString);
con.Open();
SqlCommand cmd = new SqlCommand("UserLogin", con);
cmd.CommandType =CommandType.StoredProcedure;
SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@UserId",name),
new SqlParameter("@Password",pwd),
};
cmd.Parameters.AddRange(para);
SqlParameter mypara =cmd.Parameters.Add("@reValue", SqlDbType.NVarChar);
mypara.Direction =ParameterDirection.ReturnValue;
cmd.ExecuteScalar();
reValue = (string)cmd.Parameters["@reValue"].Value.ToString();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex2)
{
Console.WriteLine(ex2.Message);
}
return reValue.Equals("1");
}
12、实体类在Windows程序中的高级应用,多个实体如何关联,如何在查询时显示多个实体的信息
添加辅助列:根据Id获取科目名称,年级Id获取年级名称的方法
#region根据辅助列显示
publicvoidSetStuNameAndGradeName()
{
foreach (DataGridViewRow item indgvResult.Rows)
{
String subNo = item.Cells["SubjectId"].Value.ToString();
item.Cells["SubjectName"].Value = SubjectManager.GetSubjectDataBySubjectId(int.Parse(subNo)).SubjectName;
String stuNo = item.Cells["StuNo"].Value.ToString();
item.Cells["StuName"].Value= StudentManager.getStudentNameById(int.Parse(stuNo));
}
}
#endregion
使用DataGridViewComboBoxColumn(绑定列的DataPropertyName为StudentNo和SubjectNo能自动匹配)
#region根据datagridViewCBo
publicvoidSetStuNameAndGradeName2()
{
DataGridViewComboBoxColumn cbo = (DataGridViewComboBoxColumn)this.dgvResult.Columns["StuNo"];
cbo.DataSource = StudentManager.GetStudentData();
cbo.DisplayMember = "StudentName";
cbo.ValueMember = "StudentNo";
cbo.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
cbo = (DataGridViewComboBoxColumn)this.dgvResult.Columns["SubjectId"];
cbo.DataSource = SubjectManager.getAllSubject();
cbo.DisplayMember = "SubjectName";
cbo.ValueMember = "SubjectNo";
cbo.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
}
#endregion
扩展实体类:添加ResultBusine业务实体类,studentName,SubjectName,然后连表查询。