题目一:编写C#程序:在D盘根目录下创建一个以自己的学号命名的目录,然后在该目录下创建一个(自己学号.txt)的文本文件,在该文件中写入自己的学号、姓名、学校、专业、英语成绩等信息;然后再读出文件中的信息显示到屏幕上。
源程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Exer5_1
{
internal class Program
{
static void Main(string[] args)
{
DirectoryInfo di = Directory.CreateDirectory(@"d:\\19200135105");
string path = "d:\\19200135105";
string fileName = "19200135105.txt";
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
System.IO.File.AppendText(System.IO.Path.Combine(path, fileName)).Close();
string text = "姓名:李玮烨,\n学号:19200135105,\n学校:苏州科技大学,\n专业:计算机科学与技术,\n英语成绩:85";
System.IO.File.WriteAllText(@"d:\\19200135105\\19200135105.txt", text);
Console.WriteLine("写入成功");
FileStream fs = File.OpenRead(@"d:\\19200135105\\19200135105.txt");
Byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, 0, b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
Console.ReadKey();
}
}
}
运行结果:
题目二:创建一个学生信息系统登录界面,有输入用户名、密码功能,以及具有注册的功能。在学生初次使用时,可进行注册。注册成功后,再次登录可进入系统管理界面,在系统管理界面可进行学生注册信息的查询、修改操作。
该系统提供学生的注册、及管理信息管理功能页面可由学生自行设计。要求采用SQL Server数据库,通过编程方式实现。
源程序:
注册页面:
using System;
using System.Data.SqlClient;
namespace Exer5_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//注册
private void button1_Click(object sender, EventArgs e)
{
try
{
string name = textBox1.Text.Trim();
string pwd = textBox2.Text.Trim();
//这里填写自己电脑对应的信息
string sqlcon = @"Data Source=DESKTOP-XXXXXXX;Initial Catalog=test;Integrated Security=SSPI";
SqlConnection sc = new SqlConnection(sqlcon);
string sql = "insert into studentInfo(username,pwd)" + "values(@username,@pwd)";
SqlCommand cmd = new SqlCommand(sql, sc);
SqlParameter sqlParameter = new SqlParameter("@username", textBox1.Text);
cmd.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@pwd", textBox2.Text);
cmd.Parameters.Add(sqlParameter);
sc.Open();
cmd.ExecuteNonQuery();
sc.Close();
MessageBox.Show("注册成功,您现在可以登录了");
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("请输入用户名");
}
if (textBox2.Text.Trim() == "")
{
MessageBox.Show("请输入密码");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Form2 f = new Form2();
this.Hide();
f.ShowDialog();
Application.ExitThread();
}
}
}
登录页面:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Exer5_2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
//登录
private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text.Trim();
string password = textBox2.Text.Trim();
//这里填写自己的对应信息
string sqlcon = @"Data Source=DESKTOP-XXXXXXX;Initial Catalog=test;Integrated Security=SSPI";
SqlConnection sqlConnection = new SqlConnection(sqlcon);
sqlConnection.Open();
string sql = "select username,pwd from studentInfo where username= ' " + username + "' and pwd = '" + password + "'";
SqlCommand cmd = new SqlCommand(sql, sqlConnection);
SqlDataReader sread = cmd.ExecuteReader();
try
{
if (sread.Read())
MessageBox.Show("登录成功");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
Form3 f3 = new Form3();
this.Hide();
f3.ShowDialog();
Application.ExitThread();
}
}
}
信息修改查询:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Exer5_2
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
//修改
private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text.Trim();
string pwd = textBox2.Text.Trim();
//这里填写自己电脑对应的信息
string consqlserver = @"Data Source=DESKTOP-XXXXXXX;Initial Catalog=test;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(consqlserver);
con.Open();
try
{
string insertpwd = "";
if (username != "")
insertpwd = "update studentInfo set pwd= '" + pwd + "' WHERE username = '" + username + "'";
SqlCommand cmd = new SqlCommand(insertpwd, con);
cmd.ExecuteNonQuery();
MessageBox.Show("修改成功");
}
catch
{
MessageBox.Show("输入数据违反要求");
}
finally
{
con.Close();
}
}
//查询
private void button2_Click(object sender, EventArgs e)
{
string username = textBox1.Text.Trim();
string pwd = textBox2.Text.Trim();
//这里填写自己电脑对应的信息
string consqlserver = @"Data Source=DESKTOP-XXXXXXX;Initial Catalog=test;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(consqlserver);
con.Open();
try
{
String select_by_username = "select username,pwd from studentInfo where username= ' " + username + "' and pwd = '" + pwd + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_username, con);
SqlDataReader reader = sqlCommand.ExecuteReader();
richTextBox1.Clear();
richTextBox1.Text += "用户名:" + username + "\n"+"密码:" + pwd;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句");
}
}
}
}
运行结果:
注册:
对应数据库新增一条记录:
登录:
修改信息:
查看是否修改成功:
查询: