c五个小程序.docx

上传人:wj 文档编号:1215695 上传时间:2023-04-30 格式:DOCX 页数:28 大小:370.58KB
下载 相关 举报
c五个小程序.docx_第1页
第1页 / 共28页
c五个小程序.docx_第2页
第2页 / 共28页
c五个小程序.docx_第3页
第3页 / 共28页
c五个小程序.docx_第4页
第4页 / 共28页
c五个小程序.docx_第5页
第5页 / 共28页
c五个小程序.docx_第6页
第6页 / 共28页
c五个小程序.docx_第7页
第7页 / 共28页
c五个小程序.docx_第8页
第8页 / 共28页
c五个小程序.docx_第9页
第9页 / 共28页
c五个小程序.docx_第10页
第10页 / 共28页
c五个小程序.docx_第11页
第11页 / 共28页
c五个小程序.docx_第12页
第12页 / 共28页
c五个小程序.docx_第13页
第13页 / 共28页
c五个小程序.docx_第14页
第14页 / 共28页
c五个小程序.docx_第15页
第15页 / 共28页
c五个小程序.docx_第16页
第16页 / 共28页
c五个小程序.docx_第17页
第17页 / 共28页
c五个小程序.docx_第18页
第18页 / 共28页
c五个小程序.docx_第19页
第19页 / 共28页
c五个小程序.docx_第20页
第20页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c五个小程序.docx

《c五个小程序.docx》由会员分享,可在线阅读,更多相关《c五个小程序.docx(28页珍藏版)》请在冰点文库上搜索。

c五个小程序.docx

界面

(一)

1.练习目的

熟悉lable、butten、textbox、radiobutten等控件的使用

2.程序代码

privatevoidbutton1_Click(objectsender,EventArgse)

{

if(DX.Checked==true)

{

this.textBox4.Text="姓名:

"+this.textBox1.Text+"\r\n"+"性别:

"+this.textBox2.Text+"\r\n"

+"学号:

"+this.textBox3.Text+"\r\n"+"爱好:

"+this.DX.Text;}

if(ZH.Checked==true)

{

this.textBox4.Text="姓名:

"+this.textBox1.Text+"\r\n"+"性别:

"+this.textBox2.Text+"\r\n"

+"学号:

"+this.textBox3.Text+"\r\n"+"爱好:

"+this.ZH.Text;

}

if(KC.Checked==true)

{

this.textBox4.Text="姓名:

"+this.textBox1.Text+"\r\n"+"性别:

"+this.textBox2.Text+"\r\n"+"学号:

"+this.textBox3.Text+"\r\n"+"爱好:

"+this.KC.Text;

}

3.程序界面

4.运行结果

界面

(二)

1.练习目的

熟悉toolStripMenuItem、panel等窗体的功能及使用方法,学会演出新窗体的代码,以及退出窗体的代码。

2.代码

privatevoidbutton1_Click(objectsender,EventArgse)

{

Form2form=newForm2();

form.Show();//第一个窗体不隐藏

//this.close();第一个窗口隐藏

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

this.Close();

}

3.窗体设计

}

4.实验结果

窗口传值

1.练习目的

学会在两个窗口之间进行传值的代码,并熟练运用。

2.代码

Form1

privatevoidbutton1_Click(objectsender,EventArgse)

{

if(DX.Checked==true)

{

this.textBox4.Text="姓名:

"+this.textBox1.Text+"\r\n"+"性别:

"+this.textBox2.Text+"\r\n"

+"学号:

"+this.textBox3.Text+"\r\n"+"爱好:

"+this.DX.Text;

}

if(ZH.Checked==true)

{

this.textBox4.Text="姓名:

"+this.textBox1.Text+"\r\n"+"性别:

"+this.textBox2.Text+"\r\n"

+"学号:

"+this.textBox3.Text+"\r\n"+"爱好:

"+this.ZH.Text;

}

if(KC.Checked==true)

{

this.textBox4.Text="姓名:

"+this.textBox1.Text+"\r\n"+"性别:

"+this.textBox2.Text+"\r\n"

+"学号:

"+this.textBox3.Text+"\r\n"+"爱好:

"+this.KC.Text;

}

}

privatevoidtextBox1_TextChanged(objectsender,EventArgse)

{

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

Form2f2=newForm2();

f2.ShowDialog(this);

}

}

Form2

publicpartialclassForm2:

Form

{

publicForm2()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

Form1f1=(Form1)this.Owner;

this.textBox1.Text=f1.textBox4.Text;

}

}

3.窗体设计

4.运行结果

数据库

(一)

一、实验目的:

学习使用c#进行可视化数据库保存,查询。

二、实验步骤:

1.建立项目DataBase

2.在解决方案资源管理器中,Database上点右键,选择添加—>新建项

3.在弹出的窗口中选择数据à基于服务的数据库,点击添加,然后点击完成

4.在菜单栏视图中打开服务器资源管理器,在“表”上点击右键,选择添加新表

5.按下图新建一个表并在行前点击右键来设置主键,点击保存 并输入一个表名

6.在刚才新建的表上点击右键,选择显示表数据

7.在表中输入如下的数据并保存

8.点击菜单数据à显示数据源,在database1dataset上点击右键选择使用向导配置数据集,选中表,点击完成

9.点击菜单生成—>生成解决方案,会在工具箱中生成新的控件,在解决方案资源管理器中生成链接字符串

10.在界面上添加一个datagridview控件和两个button,一个是保存,一个是查询

11.添加命名空间引用

usingSystem.Data.SqlClient;

12..给form1类添加两个变量

SqlDataAdapteradapter;

DataTabletable;

13.给Form1添加一个Form1_Load事件,事件中添加代码如下

stringconnStr=Properties.Settings.Default.MyDatabaseConnectionString;

SqlConnectionconn=newSqlConnection(connStr);

adapter=newSqlDataAdapter("select*fromMyTable",conn);

SqlCommandBuilderbuilder=newSqlCommandBuilder(adapter);

adapter.InsertCommand=builder.GetInsertCommand();

adapter.DeleteCommand=builder.GetDeleteCommand();

adapter.UpdateCommand=builder.GetUpdateCommand();

table=newDataTable();

adapter.Fill(table);

dataGridView1.DataSource=table;

14.给按钮查询添加一个click事件响应函数,添加如下代码

stringvarNumber=textBoxNumber.Text.Trim();;

if(varNumber=="")

{

MessageBox.Show("请输入你要查询的学号","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);

}

try

{

stringconnStr=Properties.Settings.Default.MyDatabaseConnectionString;SqlConnectionconn=newSqlConnection(connStr);

adapter=newSqlDataAdapter("select*fromMyTablewhere学号='"+varNumber+"'",conn);

SqlCommandBuilderbuilder=newSqlCommandBuilder(adapter);

adapter.InsertCommand=builder.GetInsertCommand();

adapter.DeleteCommand=builder.GetDeleteCommand();

adapter.UpdateCommand=builder.GetUpdateCommand();

table=newDataTable();

adapter.Fill(table);

dataGridView1.DataSource=table;

//conn.Close();

}

catch(Exceptionee)

{

MessageBox.Show(ee.Message,"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);

}

15.给保存按钮添加一个click事件响应函数,添加如下代码

textBoxNumber.Text="";

dataGridView1.EndEdit();

try

{

adapter.Update(table);

MessageBox.Show("保存成功");

}

catch(Exceptionee)

{

MessageBox.Show(ee.Message,"保存失败");

}

MyLoad();

三、实验结果:

1.可输入信息状态

2.保存

3.查询保存结果

数据库

(二)

1.实训目的:

1.了解ADO.NET结构

2.了解ADO.NET的组件

3.使用Command对象和Connection对象

2.功能要求

使用VisualStudio实现简单注册和登录功能,SQLServer作为后台数据库。

程序运行界面如图-1(注册)和图-2(登录)所示。

图-1程序运行效果(注册)

图-2程序运行效果(登录)

程序的功能要求如下:

1.在SQLServer中创建数据库、表结构,并输入测试数据。

2.按照图-1设计程序窗体界面,并完成以下功能:

1)新建两个窗体,在窗体界面上添加并设置控件,如图-1和图-2所示;

2)点击“注册”,即可添加一条数据并显示在列表中;

3)点击”登录”,如果登录成功则使用MessageBox中的方法显示成功的登录用户名

3.数据库设计

数据库名称:

RegistAndLogin

数据表名

Users

字段显示

字段名

数据类型

字段大小

备注和说明

编号

userId

int

主键,自增1

姓名

userName

varchar

30

不允许空

密码

userPwd

varchar

20

邮箱

userEmail

varchar

50

电话

userPhone

int

4.实现注册

1)使用SqlCommand对象将数据添加到数据库中。

当点击注册按钮后需要给予用户提示,“注册成功!

”或“注册失败!

”。

如图-3用户注册

2)密码输入框内输入密码要为“*”

图-3用户注册

5.实现登录

1)使用DataAdapter对象将数据填充到DataSet中并返回DataSet,判断DataSet是否存在数据。

2)当用户单击登录时,如果输入的用户名和密码错误则弹出提示“用户登录失败”,用户输入用户名和密码正确则将记录集中的用户名读出来,弹出如图-3用户登录界面提示

3)密码输入框内输入密码要为“*”

图-3用户登录

6.主要代码与程序截图

登录

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

namespaceLoginAndRegister

{

publicpartialclasslogin:

Form

{

publiclogin()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

if(this.username.Text==""||this.password.Text=="")

MessageBox.Show("请输入用户名和密码");

else

{

SqlConnectioncon=newSqlConnection();//创建连接

con.ConnectionString="server=(local);database=RegistAndLogin;uid=sa;pwd=123456";

con.Open();

SqlCommandcmd=con.CreateCommand();

cmd.CommandText="select*fromUsers";

SqlDataReaderdr=cmd.ExecuteReader();

if(dr.Read())

{

stringpower=dr[0].ToString();

indexmain=newindex();

main.Tag=this.FindForm();

this.Visible=false;

main.ShowDialog();

this.Close();

}

else

{

MessageBox.Show("用户名或密码错误!

");

}

con.Close();

}

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

registerregister=newregister();

register.Show();

}

}

}

注册

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

namespaceLoginAndRegister

{

publicpartialclassregister:

Form

{

publicregister()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

if(this.username.Text!

="")

{

SqlConnectioncon=newSqlConnection();

con.ConnectionString="server=(local);database=RegistAndLogin;uid=sa;pwd=123456";

SqlCommandcmd=con.CreateCommand();

cmd.CommandText="insertintoUsers(userName,userPwd,userEmail,userPhone)"+"values('"+username.Text+"','"+password.Text+"','"+email.Text+"','"+phone.Text+"')";

con.Open();

intdd=cmd.ExecuteNonQuery();

if(dd==1)

{

MessageBox.Show("添加成功!

");

}

}

else

MessageBox.Show("请输入用户名!

");

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2