实验8文件操作
1 实验目的
(1)理解文件与流的基本概念。
(2)了解目录与文件的基本知识及其常用的类。
(3)掌握文件流读写方法。
2.实验设备
硬件:PC 机;
软件:Visual Studio开发环境,Windows 操作系统。
3.实验内容
(1)点击“创建”按钮创建一个txt文件,如果已有该文件则提示用户已有该文件,否则提示创建成功
在添加文件内容内输入添加的文字,按“添加”按钮后将该段文字保存到txt文件
按“详细信息”按钮后显示文件的详细信息
参考 P152【例5-4】 和 P155【例5-5】
(2)二进制文件的读取和保存
P160程序设计题
4.实验原理
文件与流
目录操作
文件操作
文件读写
5. 实验参考程序
private void btnCreate_Click(object sender, EventArgs e){
//获取输入文件名称
string fileName = txtFileName.Text;
//判断文件是否存在
if (isFile(fileName)){MessageBox.Show("文件已存在!");}
else{
//创建文件后Close关闭文件流
File.Create(fileName).Close();
MessageBox.Show("创建成功!");
}
}
private void btnAdd_Click(object sender, EventArgs e){
//获取输入内容
string content = txtFileContent.Text;
//没有内容不添加
if (content == "") return;
//判断文件是否存在
string fileName = txtFileName.Text;
if (!isFile(fileName)){
MessageBox.Show("当前输入文件不存在,无法添加!");return;
}
StreamWriter writer = new StreamWriter(fileName, true);
writer.WriteLine(content);
writer.Close();
MessageBox.Show("添加成功!");
}
private void btnFileInfo_Click(object sender, EventArgs e){
//判断文件是否存在
string fileName = txtFileName.Text;
if (!isFile(fileName)){
MessageBox.Show("当前输入文件不存在,无法查看详情!");
return;
}
//获取文件实例
FileInfo f = new FileInfo(fileName);
txtDirectory.Text = f.Directory.ToString();
txtSize.Text = f.Length.ToString();
}
//判断文件是否存在
private bool isFile(string path){return File.Exists(path);}
实验9非断开式访问数据库
1 实验目的
(1)掌握SQL的基本语句。
(2)掌握C#与SQL Server数据库的连接方法。
(3)掌握非断开式访问数据库方法。理解文件与流的基本概念。
2.实验设备
硬件:PC 机;
软件:Visual Studio开发环境,Windows 操作系统。
3.实验内容
(1)建立数据库studentmanage,并建立数据表studentinfo(num,nam建立数据库e,sex)。按教材表6-1添加记录
(2)参考例6-2,6-3实现数据库的连接、查询、增加、删除功能,界面如图所示。
4.实验原理
非断开式的访问数据库,在取得数据库连接之后将一直保持与数据库的连接,直到执行关闭连接的操作。具体步骤如下:
(1)通过数据库连接类(Connection)连接到数据库。
(2)通过数据库命令类(Command)在数据库上执行SQL语句,实现对数据库的插入(Insert)、删除(Delete)、更新(Update)以及查询(Select)等操作。
(3)如果是查询操作,可以通过数据库读取器类(DataReader)进行数据记录的向前只读操作。
(4)数据库操作完成后,再通过连接类(Connection)关闭当前的数据连接。
在非断开式访问数据库过程中,由于数据库的客户连接数量有限,因此,应尽量缩短与数据库操作的时间,数据库操作一旦完成,应及时关闭对应的数据库连接。
5. 实验参考程序
private void ShowCurrentStudent()
{
string sql = "select * from studentInfo where num=" + current;
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
txtNum.Text = reader.GetInt32(0).ToString();
txtName.Text = reader.GetString(1);
int sex = reader.GetInt32(2);
if (sex == 0)
{
rdbMale.Checked = true;
}
else
{
rdbFemale.Checked = true;
}
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
int sex = 0;
if (rdbFemale.Checked)
{
sex = 1;
}
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("update studentInfo set name=@name,sex=@sex where num=@num", con);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@sex", sex);
cmd.Parameters.AddWithValue("@num", Convert.ToInt32(txtNum.Text));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("成功修改一条记录!");
}
实验10断开式访问数据库
1 实验目的
了解DataAdapter 和DataSet类,掌握断开式访问数据库方法。
2.实验设备
硬件:PC 机;
软件:Visual Studio开发环境,Windows 操作系统。
3.实验内容
(1)在数据库studentmanage中建立数据表studentscore(num,name,score)。按教材表6-2添加记录
(2)参考例6-4实现数据库的查询、增加、修改、删除功能,界面如图所示。
4.实验原理
断开式的访问数据库,当数据适配器从数据源得到数据,或是从数据集更新数据到数据源后,其与数据源的连接就马上断开。具体步骤如下:
(1)通过数据库连接类(Connection)连接到数据库。
(2)创建基于当前数据库连接的数据适配器(DataAdaper),数据适配器通过数据库命令类(Command)从数据库获取数据到本地数据集(DataSet)中。
(3)对本地数据集中的数据进行插入(Insert)、删除(Delete)、更新(Update)以及查询(Select)等操作。
(4)通过适配器将本地数据更新到数据库。。
5. 实验参考程序
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
private void btnSelect_Click(object sender, EventArgs e)
{
con = new SqlConnection(conStr);
da = new SqlDataAdapter("select * from studentscore where num=2", con);
ds = new DataSet();
da.Fill(ds, "studentscore");
//DataRowCollection rows = ds.Tables["studentscore"].Rows;
DataRow row = ds.Tables["studentscore"].Rows[0];
string s = string.Format("{0}, {1}", row["name"].ToString().Trim(), row["score"]);
dgvStudentScore.DataSource = ds.Tables["studentscore"];
}
private void bbtnSelect_Click(object sender, EventArgs e)
{
con = new SqlConnection(conStr);
da = new SqlDataAdapter("select * from studentscore", con);
ds = new DataSet();
da.Fill(ds, "studentscore");
dgvStudentScore.DataSource = ds.Tables["studentscore"];
}
private void btnInsert_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataRow r1 = ds.Tables["studentscore"].NewRow();
r1[0] = txtNum.Text;
r1[1] = txtName.Text;
r1[2] = txtScore.Text;
ds.Tables[0].Rows.Add(r1);
da.Update(ds, "studentscore");
dgvStudentScore.DataSource = ds.Tables["studentscore"];
}
private void btnUpdate_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataRowCollection rows = ds.Tables["studentscore"].Rows;
DataRow row;
for (int i = 0; i < rows.Count; i++)
{
row = rows[i];
if (row["num"].ToString() == txtNum.Text)
{
row["score"] = Int32.Parse(txtScore.Text);
}
}
dgvStudentScore.DataSource = ds.Tables["studentscore"];
da.Update(ds, "studentscore");
}
private void btnDelete_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataRowCollection rows = ds.Tables["studentscore"].Rows;
DataRow row;
for (int i = 0; i < rows.Count; i++)
{
row = rows[i];
if (row["num"].ToString() == txtNum.Text)
{
row.Delete();
}
}
da.Update(ds, "studentscore");
dgvStudentScore.DataSource = ds.Tables["studentscore"];
}