数据库应用:
*需要提交:
1、创建的数据库以及每个项目的源文件;
2、整个实验提交一份实验报告,含主要代码片断以及程序截图
1.设计一个检验密码程序,当用户在文本框中输入密码按回车键时,如果用户输入密码正确,则在窗体上的标签中显示“欢迎你使用本系统!”,否则,显示“密码错误,请重新输入密码。”,若连续三次输入的密码都不正确,则在屏幕上显示一个消息框,并在消息框中提示信息“对不起,你不能使用本系统!”,然后结束本程序的执行。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace桌面3_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
i = i + 1;
if (textBox1.Text == "123")
{
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
g.DrawString("欢迎登陆", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();
}
else
{
if (i < 3)
{
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
g.DrawString("密码错误,请重新输入密码。", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();
}
else
{
Form2 fm = new Form2();
fm.Show();
}
Form2 fm = new Form2();
fm.Show();
}
}
}
}
2.设计一个程序,查询飞往各城市的航班时间。功能是:在运行程序后,用户从列表框中选择一个城市名,然后,单击“查询”按钮,在文本框中显示航班的起飞时间。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace桌面3_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.GetSelected (0) )
{
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
g.DrawString("起飞时间是9:27", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();
}
if (listBox1.GetSelected(1))
{
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
g.DrawString("起飞时间是19:20", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();
}
if (listBox1.GetSelected(2))
{
Graphics g = panel1.CreateGraphics();
g.Clear(Color.Gray);
g.DrawString("起飞时间是22:07", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();
}
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("北京");
listBox1.Items.Add("上海");
listBox1.Items.Add("广州");
}
}
}
3.
3.编写一个同学通信录程序:当在下拉列表框中选择某一同学的姓名后,能够在三个文本框中分别显示该同学的电话号码、邮政编码和住址。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace桌面3_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (comboBox1.SelectedItem == "张三")
{
{
Graphics g = panel1.CreateGraphics();
g.Clear(Color.White );
g.DrawString("电话号码:132********", new Font("宋体", 9), new
SolidBrush(Color.Black), 0, 0);
g.Dispose();
}
{
Graphics a = panel2.CreateGraphics();
a.Clear(Color.White);
a.DrawString("邮政编码:201209", new Font("宋体", 9), new SolidBrush(Color.Black), 0,
0);
a.Dispose();
}
{
Graphics s = panel3.CreateGraphics();
s.Clear(Color.White);
s.DrawString("住址:河东女寝", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
s.Dispose();
}
}
if (comboBox1.SelectedItem == "李四")
{
{ Graphics g = panel1.CreateGraphics();
g.Clear(Color.White);
g.DrawString("电话号码:1520957363", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();}{
Graphics g = panel2.CreateGraphics();
g.Clear(Color.White);
g.DrawString("邮政编码:051430", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();}{
Graphics g = panel3.CreateGraphics();
g.Clear(Color.White);
g.DrawString("住址:河东男寝", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();}
}
if (comboBox1.SelectedItem == "王五")
{
{Graphics g = panel1.CreateGraphics();
g.Clear(Color.White);
g.DrawString("电话号码:150017771946", new Font("宋体", 9), new
SolidBrush(Color.Black), 0, 0);
g.Dispose();}
{ Graphics g = panel2.CreateGraphics();
g.Clear(Color.White);
g.DrawString("邮政编码:050000", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();}
{ Graphics g = panel3.CreateGraphics();
g.Clear(Color.White);
g.DrawString("住址:河南女寝", new Font("宋体", 9), new SolidBrush(Color.Black), 0, 0);
g.Dispose();}
}
}
}
}
4.文件you.mdb是Access数据库文件。编写程序,运行该程序,可以在窗体的四个角上显示you.mdb数据库中you数据表的第一条记录中的一个you字段中的内容。要求显示的文字大小和颜色互不相同。另外,在窗体的中间显示你的姓名。程序的显示可以随you字段中的内容的变化而变化。(you.mdb数据库自建)
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using https://www.doczj.com/doc/6e17431134.html,mon;
namespace _4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string SelectString = "select * from you";
https://www.doczj.com/doc/6e17431134.html,mandText = SelectString;
dataSet11.Clear();
oleDbDataAdapter1.Fill(dataSet11, "you");
label1.Text = dataSet11.Tables[0].Rows[0][0].ToString();
label2.Text = dataSet11.Tables[0].Rows[0][0].ToString();
label3.Text = dataSet11.Tables[0].Rows[0][0].ToString();
label4.Text = dataSet11.Tables[0].Rows[0][0].ToString();
}
5.建一个到sql server数据库northwind的连接,并将customers表中的ContactName字段的值添加到列表框中。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Data05
{
public partial class Form1 : Form
{
string strCon;
SqlConnection sqlCon;
public Form1()
{
InitializeComponent();
}
private void DBConnect()
{
strCon = "server=FANJL-PC;Initial Catalog=northwind;integrated
security=true;database=northwind;uid=FANJL-PC\fanjl";
sqlCon = new SqlConnection(strCon);
}
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
DBConnect();
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from customers ",sqlCon);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
for (i = 0; i
{
listBox1.Items.Add(dt.Rows[i]["ContactName"]).ToString();
}
sqlCon.Close();
}
}
}
6.创建一个数据库应用程序,启动该应用程序如下图所示,列表框中显示的是sql server 数据库northwind中customers表的ContactName字段的值,当单击列表框中的某一项时,该项目对应于customers表中的唯一记录的记录的信息显示在窗体左边对应的文本框中,也就是说通过列表框可浏览customers表中每一条记录的信息。
7.继续上题,在窗体上添加3个按钮[添加]、[修改]、[删除],分别对这3个按钮编写事件处理程序,通过窗体界面实现对customers表[添加]、[修改]、[删除]记录。
8.创建一个数据库查询应用程序,启动该应用程序如下图所示,组合框中装载sql server 数据库northwind中产品表的每一条记录的产品名称字段的值,通过选择组合框中产品名称,可查询该产品对应的信息并显示在窗体上相应的文本框中。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string selectstring = "select * from products";
https://www.doczj.com/doc/6e17431134.html,mandText = selectstring;
dataSet11.Clear();
oleDbDataAdapter1.Fill(dataSet11, "products");
for (int i = 0; i < dataSet11.Tables[0].Rows.Count; i++)
{
comboBox1.Items.Add(dataSet11 .Tables[0].Rows[i]["产品名称"].ToString ());//把数据库中的产品名称加到组合框中
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int n = comboBox1.SelectedIndex;
textBox1.Text = dataSet11.Tables[0].Rows[n]["产品ID"].ToString();
textBox2.Text = dataSet11.Tables[0].Rows[n]["产品名称"].ToString();
textBox3.Text = dataSet11.Tables[0].Rows[n]["单价"].ToString();
textBox4.Text = dataSet11.Tables[0].Rows[n]["库存"].ToString();
textBox5.Text = dataSet11.Tables[0].Rows[n]["订单数量"].ToString();
string x = dataSet11.Tables[0].Rows[n]["折扣"].ToString();
if (x == "0")
{
checkBox1.Checked = true;
}
else
{
checkBox1.Checked = false;
}
}
9.创建一数据库应用程序,启动该应用程序如下图所示,通过单击[第一条]、[前一条]、[后一条]、[最后一条]可浏览sql server 数据库northwind中产品表的信息。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Data09
{
public partial class Form1 : Form
{
string strCon;
SqlConnection sqlCon;
public Form1()
{
InitializeComponent();
}
private void DBConnect()
{
strCon = "server=FANJL-PC;Initial Catalog=northwind;integrated security=true;database=northwind;uid=FANJL-PC\fanjl";
sqlCon = new SqlConnection(strCon);
}
private void button1_Click(object sender, EventArgs e)
{
DBConnect();
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from product",sqlCon);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
textBox1.Text = dt.Rows[0][0].ToString();
textBox2.Text = dt.Rows[0][1].ToString();
textBox3.Text = dt.Rows[0][2].ToString();
textBox4.Text = dt.Rows[0][3].ToString();
textBox5.Text = dt.Rows[0][4].ToString();
sqlCon.Close();
}
int i = 0;
private void button2_Click(object sender, EventArgs e)
{
DBConnect();
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from product", sqlCon);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
if (i == -1)
i = i +1;
if (i < dt.Rows.Count&&i>-1)
{
textBox1.Text = dt.Rows[i][0].ToString();
textBox2.Text = dt.Rows[i][1].ToString();
textBox3.Text = dt.Rows[i][2].ToString();
textBox4.Text = dt.Rows[i][3].ToString();
textBox5.Text = dt.Rows[i][4].ToString();
i++;
}
else
{
MessageBox.Show("已经是最后一条!");
button2.Enabled = false;
button3.Enabled = true;
}
sqlCon.Close();
}
private void button3_Click(object sender, EventArgs e)
{
DBConnect();
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from product", sqlCon);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
if (i == dt.Rows.Count)
i = i - 1;
if (i > -1&&i
{
textBox1.Text = dt.Rows[i][0].ToString();
textBox2.Text = dt.Rows[i][1].ToString();
textBox3.Text = dt.Rows[i][2].ToString();
textBox4.Text = dt.Rows[i][3].ToString();
textBox5.Text = dt.Rows[i][4].ToString();
i--;
}
else
{
MessageBox.Show("已经是第一条!");
button3.Enabled = false;
button2.Enabled = true;
}
sqlCon.Close();
}
private void button4_Click(object sender, EventArgs e)
{
DBConnect();
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from product", sqlCon);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
textBox1.Text = dt.Rows[dt.Rows.Count-1][0].ToString();
textBox2.Text = dt.Rows[dt.Rows.Count - 1][1].ToString();
textBox3.Text = dt.Rows[dt.Rows.Count - 1][2].ToString();
textBox4.Text = dt.Rows[dt.Rows.Count - 1][3].ToString();
textBox5.Text = dt.Rows[dt.Rows.Count - 1][4].ToString();
sqlCon.Close();
}
}
}
10.
10.程序窗体中放入一个文本框和一个“统计”按钮。该程序计算northwind数据库的products 表中产品的平均价格并显示在一个文本框。该程序运行时的画面如下。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Data10
{
public partial class Form1 : Form
{
string strCon;
SqlConnection sqlCon;
public Form1()
{
InitializeComponent();
}
private void DBConnect()
{
strCon = "server=FANJL-PC;Initial Catalog=northwind;integrated security=true;database=northwind;uid=FANJL-PC\fanjl";
sqlCon = new SqlConnection(strCon);
}
private void button1_Click(object sender, EventArgs e)
{
DBConnect();
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from product",sqlCon);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
int sum = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
sum+=int.Parse(dt.Rows[i]["price"].ToString());
}
textBox1.Text = (sum / dt.Rows.Count).ToString();
sqlCon.Close();
}
}
}
11.下面的例子给STUDENTS数据表插入记录。为此,我们需要建立一个SCHOOL数据库以及一个STUDENTS表。该表有SNO、NAME、AGE、SEX等字段,表示学号、姓名、年龄、性别。除了AGE为int类型外,其余都是char类型的,并且要将SNO设置为主键(Primary Key)。
程序窗体中放入两个文本框、一个NumericUpDown和两个单选按钮,这些控件分别用于输入学号、姓名、年龄、性别。另用四个标签来说明这几个控件以及一个“插入”按钮来执行插入记录的操作。
NumericUpDown是一种带数值微调按钮的组合框,本例中我们设置它的Maximun属性为30,Minimum为15。运行时程序可以读取它的Value属性值。
为了不必在每次插入记录时重新创建数据库组件,我们将这些组件声明为程序窗体的成员。此外,程序应添加对System.Data.SqlClient的引用。该程序运行时画面如下图。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string selectstring = "select * from Scores";
https://www.doczj.com/doc/6e17431134.html,mandText = selectstring;
dataSet11.Clear();
oleDbDataAdapter1.Fill(dataSet11, "Scores");
}
}
}
12.在SCHOOL数据库中创建Scores数据表。如下图,该表有SNO、CNO、SCORE等列
(分别表示学号、课程号、成绩),可用于记录学生成绩。创建完成后在该表中输入十来条
记录。
在应用程序窗体Form1中加入必要的数据库组件与Scores表相连接,并加上DataGrid 以实现对该表进行只读浏览。要求DataGrid显示时列标题显示为汉字。
using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/6e17431134.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string selectstring = "select * from Scores";
https://www.doczj.com/doc/6e17431134.html,mandText = selectstring;
dataSet11.Clear();
oleDbDataAdapter1.Fill(dataSet11, "Scores");
}
}
}