C复习 下载本文

A. 输出 Exception Finally B. 输出 Finally

C. 输出 Exception D. 无输出

【例5】下列代码中,哪些行将产生空指针异常。(A)

1 string s = null;

2 if ( s != null || s.Length > 0)

3 Console.WriteLine("s != null || s.Length > 0"); 4 if ( s != null | s.Length > 0)

5 Console.WriteLine("s != null | s.Length > 0"); 6 if ( s != null & s.Length > 0)

7 Console.WriteLine("s != null & s.Length > 0"); 8 if ( s != null && s.Length > 0)

Console.WriteLine("s != null && s.Length > 0");

A.2 4 6 B.2 4 6 8

C.4 6 8 D.2 6 8

三、windows窗体设计

1、委托的定义及使用。

2、C#使用委托机制实现事件处理。

3、计时器控件。

【例1】在一个窗体应用程序中,需要在一分钟内自动浏览完文件夹中的60张图片,则需要设置计时器控件的Interval属性的值为 _____1000_______。

四、数据库连接

1、通过在ADO.NET框架中,通过创建Connection对象建立与数据源之间的连接,通过创建Command对数据源执行各种SQL命令。

string conStr = "data source = 127.0.0.1; database = studentcourse; integrated security = True";

SqlConnection sqlCon = new SqlConnection(conStr);

sqlCon.Open();

string userName = txtUserName.Text.Trim();

string sqlStr = "select userName,userPass from userInfo where userName = '" + userName + "'";

SqlCommand sqlCommand = new SqlCommand(sqlStr,sqlCon);

string sqlStr = “select * from student”;

SqlCommand command

= new SqlCommand(sqlStr,sqlCon) ;

2、MD5算法。

思考:如何使用后台数据库中的userInfo(userName,userPass)表实现用户的登录与注册?其中,userPass字段中存放的是由MD5算法加密后的用户密码。

/*使用MD5加密字符串的方法*/

public static string GetMd5Str(string myString)

{

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] fromData = Encoding.Unicode.GetBytes(myString);

byte[] toData = md5.ComputeHash(fromData);

string byteStr = null;

for (int i = 0; i < toData.Length; i++)

{

byteStr += toData[i].ToString("x");

}

return byteStr;

}

/* btnAdd_Click 方法实现用户信息的注册*/

private void btnAdd_Click(object sender, EventArgs e)

{

string userName = txtUser.Text.Trim();

string userPass = txtPass.Text.Trim();

string conStr = "server=.;database = studentcourse;integrated security = True";

SqlConnection sqlCon = new SqlConnection(conStr);

sqlCon.Open();

SqlCommand checkCmd = sqlCon.CreateCommand();

checkCmd.CommandText = "select * from userInfo where userName = @un ";

checkCmd.Parameters.AddWithValue("un", userName);

SqlDataReader reader = checkCmd.ExecuteReader();

if (reader.Read())

{

MessageBox.Show("用户名已经存在");

}

else

{

reader.Dispose();

string sqlStr = "insert into

userInfo(userName,userPass)values(@un,@pd)";

SqlCommand sqlCommand = new SqlCommand(sqlStr, sqlCon); sqlCommand.Parameters.AddWithValue("un", userName); sqlCommand.Parameters.AddWithValue("pd",

GetMd5Str(userPass));

sqlCommand.ExecuteNonQuery();

MessageBox.Show("添加成功", "提示");

}

sqlCon.Close();

}

/* btnOK_Click 方法实现用户的登录/

private void btnOK_Click(object sender, EventArgs e)

{

string conStr = "server=.;database = studentcourse;integrated security = True"; SqlConnection sqlCon = new SqlConnection(conStr);

sqlCon.Open();

string sqlStr = "select userName,userPass from userInfo where userName = @userName";

SqlCommand sqlCommand = new SqlCommand(sqlStr, sqlCon);

sqlCommand.Parameters.AddWithValue("@userName", txtUser.Text.Trim()); SqlDataReader reader = sqlCommand.ExecuteReader();

{

MessageBox.Show("用户名不存在!", "错误");

}

else if (reader["userPass"].ToString().Trim() == {

MessageBox.Show("登录成功", "正确");

}

else

{

MessageBox.Show("密码错误", "错误");

}

sqlCon.Close();

}

3、SQL语句的注入攻击。

【例1】请举例说明什么是SQL语句注入攻击?试给出一种预防注入攻击的方法。

答:假设使用如下SQL语句查找给定学号的学生信息,

String s = “select * from student where sno=?” + sno + “?”; 注意引号 Sno代表用户输入的学号。如果输入的是字符串:? or ?1? =?1

那么,执行该语句,将返回student表中的所有的学生信息。即该语句存在SQL语句的注入攻击。

可以采用传入参数的方式预防注入攻击,对应的代码改成:

string sqlStr = "select sno,sname from student where sno = @snoPar"; SqlCommand command = new SqlCommand(sqlStr, sqlCon);

command.Parameters. AddWithValue ("@snoPar",sno);

此时,当用户输入字符串:? or ?1? =? , 上述代码将在student表中查找学号

为“:? or ?1? =?1”的学生信息,返回结果为空,即预防了上述的注入攻击。

4、数据库系统设计的三层架构:表示层(UI)、业务逻辑层(BLL)、数据访问层(DAL)

(三)程序设计题

1、请编写一个实现闰年测试的代码。闰年为能被4整除却不能被100整除,或能被400整除的年份。

【参考代码】

Console.WriteLine("请输入一个年份");

String sYear = Console.ReadLine();

int year;

if (int.TryParse(sYear,out year)) //把输入的字符串转换为等价的INT型 {

if (year % 400 == 0 ||year %4==0 && year % 100 != 0)

Console.WriteLine("闰年!");

else

Console.WriteLine("非闰年!");

}

2、请编写一个计算一个长整数各位数之和的方法。

【参考代码】

public static int GetSum(long s)

{

int sum = 0;

while(s > 0)

{

sum = sum + (int)(s%10);

s = s/10 ;

}

return sum;

}

3、请编写一个计算两数最大公约数的方法。

【参考代码】使用递归。

public static int F(int a, int b)

{

int c = a % b;

if (c != 0)

{

return F(b, c);

}

else

{