手机号卡销售系统c#版.docx
《手机号卡销售系统c#版.docx》由会员分享,可在线阅读,更多相关《手机号卡销售系统c#版.docx(17页珍藏版)》请在冰豆网上搜索。
手机号卡销售系统c#版
一、【实验目的】
通过编写完整的程序,掌握SqlServer各种语句的使用,添加,删除,更新等,学习程序的编写,学会分析问题,解决问题。
二、【实验内容】
通过学过的SqlServer知识,编写手机号卡销售系统,实现具体功能:
1、实现管理员的登录操作
2、创建数据库,建立管理员账号表,手机号码表,客户表
3、输入一个号码段,比如13882368,生成手机号码最后四位
4、将生成的手机号码进行分类,A类卡,比如后四位为1234,4567等,B类卡,比如后四位为1111,4444等,C类卡,比如出去A,B类卡末尾为6或者8的号码,D类卡,出去A,B,C类卡号类型的卡
5、对所选择的卡号类型进行显示,选择,查询已出售卡的信息
6、对所出售的卡号进行状态修改,将“未出售”改为“已出售”,并将客户的信息写入表中
三、【实验步骤】1、E-R图
2、登陆程序编写
①登陆窗口界面
②登陆窗口代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceWindowsApplication3
{
publicpartialclassForm1:
Form
{
publicForm1()
{//这段代码的功能是在SqlServer中创建数据库和生产表,包括管理人员表,号码表和销售表,当程序运行时,它会自动进行创建
SqlConnectionconn=newSqlConnection("server=.;userid=sa;password=;Trusted_Connection=yes;");
conn.Open();
SqlCommandmyCommand=conn.CreateCommand();
myCommand.CommandText="select*frommaster.dbo.sysdatabaseswherename='手机号码生成系统'";
SqlDataReadersdr=myCommand.ExecuteReader();
if(sdr.Read())
{
sdr.Close();
}
else
{
sdr.Close();
myCommand.CommandText="createdatabase手机号码生成系统";
myCommand.ExecuteNonQuery();
myCommand.CommandText="use手机号码生成系统";
myCommand.ExecuteNonQuery();
myCommand.CommandText="createtableworker(IDintidentityprimarykey,namevarchar(50)notnull,pwdvarchar(50)notnull)";
myCommand.ExecuteNonQuery();
myCommand.CommandText="insertintoworkervalues('wang','wang')";
myCommand.ExecuteNonQuery();
myCommand.CommandText="createtablecard(telenumbervarchar(30)primarykey,sortvarchar(30)notnull,stavarchar(30)notnull)";
myCommand.ExecuteNonQuery();
myCommand.CommandText="createtableclient(telenumbervarchar(30)primarykey,namevarchar(50)notnull,sexvarchar(10)notnull,addressvarchar(50)notnull,IDcardvarchar(30)notnull)";
myCommand.ExecuteNonQuery();
}
conn.Close();
InitializeComponent();
}
privatevoidbut2Close_Click(objectsender,EventArgse)
{
//弹出提示信息,如果点击确认退出,则退出应用程序
if(MessageBox.Show("你确定要退出系统吗?
","提示信息!
",
MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation)==DialogResult.OK)
{
Application.Exit();
}
}
privatevoidlogin_Click(objectsender,EventArgse)
{
//登陆代码,判断用户名和密码是否和数据库中的用户名和密码是否相同,如果登陆成功就弹出FORM2窗口
SqlConnectionconn=newSqlConnection("server=.;database=手机号码生成系统;uid=sa;pwd=;");
SqlCommandcmd=newSqlCommand("select*fromworkerwherename='"+textBox1.Text.Trim()+"'andpwd='"+textBox2.Text.Trim()+"'",conn);
conn.Open();
SqlDataReadersdr=cmd.ExecuteReader();
if(sdr.Read())
{
Form2form2=newForm2();
form2.ShowDialog();
Close();
}
else
{
MessageBox.Show("登陆失败!
");
this.textBox1.Text="";
this.textBox2.Text="";
}
conn.Close();
}
}
}
3、手机号码生成程序编写
①手机号码生成窗口界面
(1)号码生成
(2)号码选择
(3)出售信息
②手机号码生成代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceWindowsApplication3
{
publicpartialclassForm2:
Form
{
publicForm2()
{
InitializeComponent();
}
publicstaticstringA="A类卡";
publicstaticstringB="B类卡";
publicstaticstringC="C类卡";
publicstaticstringD="D类卡";
publicstaticstringF="未出售";
privatevoidForm2_Load(objectsender,EventArgse)
{
}
privatevoidradioButton1_Click(objectsender,EventArgse)
{
//点击选择A类卡按钮后,查找出类型为'A类卡'并且销售状态为‘未出售’
SqlConnectionconn=newSqlConnection("server=.;database=手机号码生成系统;uid=sa;pwd=;");
SqlDataAdaptersda=newSqlDataAdapter("select*fromcardwheresort='"+A+"'andsta='"+F+"'",conn);
DataTabledt=newDataTable();
sda.Fill(dt);
BindingSourcebs=newBindingSource();
bs.DataSource=dt;
dataGridView1.DataSource=bs;
bs.PositionChanged+=newEventHandler(bs_PositionChanged);
}
privatevoidbs_PositionChanged(objectsender,EventArgse)
{
//对所选择的卡号进行显示
BindingSourcebsc=(BindingSource)dataGridView1.DataSource;
DataRowViewdiv=(DataRowView)bsc.Current;
label3.Text=div["telenumber"].ToString();
}
privatevoidradioButton2_Click(objectsender,EventArgse)
{
//点击选择B类卡按钮后,查找出类型为'B类卡'并且销售状态为‘未出售’
SqlConnectionconn=newSqlConnection("server=.;database=手机号码生成系统;uid=sa;pwd=;");
SqlDataAdaptersda=newSqlDataAdapter("select*fromcardwheresort='"+B+"'andsta='"+F+"'",conn);
DataTabledt=newDataTable();
sda.Fill(dt);
BindingSourcebs=newBindingSource();
bs.DataSource=dt;
dataGridView1.DataSource=bs;
bs.PositionChanged+=newEventHandler(bs_PositionChanged);
}
privatevoidradioButton3_Click(objectsender,EventArgse)
{
//点击选择C类卡按钮后,查找出类型为'C类卡'并且销售状态为‘未出售’
SqlConnectionconn=newSqlConnection("server=.;database=手机号码生成系统;uid=sa;pwd=;");
SqlDataAdaptersda=newSqlDataAdapter("select*fromcardwheresort='"+C+"'andsta='"+F+"'",conn);
DataTabledt=newDataTable();
sda.Fill(dt);
BindingSourcebs=newBindingSource();
bs.DataSource=dt;
dataGridView1.DataSource=bs;
bs.PositionChanged+=newEventHandler(bs_PositionChanged);
}
privatevoidradioButton4_Click(objectsender,EventArgse)
{
//点击选择D类卡按钮后,查找出类型为'D类卡'并且销售状态为‘未出售’
SqlConnectionconn=newSqlConnection("server=.;database=手机号码生成系统;uid=sa;pwd=;");
SqlDataAdaptersda=newSqlDataAdapter("select*fromcardwheresort='"+D+"'andsta='"+F+"'",conn);
DataTabledt=newDataTable();
sda.Fill(dt);
BindingSourcebs=newBindingSource();
bs.DataSource=dt;
dataGridView1.DataSource=bs;
bs.PositionChanged+=newEventHandler(bs_PositionChanged);
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
Form3f3=newForm3(this.label3.Text.Trim());
f3.ShowDialog();
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
Close();
}
privatevoidbutton3_Click(objectsender,EventArgse)
{
//此处为手机号码最后四位的生成代码,生产后,会自动将所生成的电话号码插入到表中
try
{
SqlConnectionconn=newSqlConnection("server=.;database=手机号码生成系统;uid=sa;pwd=;");
SqlCommandcmd=conn.CreateCommand();
stringtel=textBox1.Text.Trim();
conn.Open();
cmd.CommandText="truncatetablecard";
cmd.ExecuteNonQuery();
for(inti=10000;i<=19999;i++)
{
inttemp1,temp2,temp3,temp4;
stringstrsub=Convert.ToString(i).Substring(1,4);
stringstr=tel+strsub;
temp1=Convert.ToInt32(str.Substring(7,1));
temp2=Convert.ToInt32(str.Substring(8,1));
temp3=Convert.ToInt32(str.Substring(9,1));
temp4=Convert.ToInt32(str.Substring(10,1));
if((temp1+1==temp2&&temp2+1==temp3&&temp3+1==temp4)||(temp1-1==temp2&&temp2-1==temp3&&temp3-1==temp4))
{
cmd.CommandText="insertintocard(telenumber,sort,sta)values('"+str+"','"+A+"','"+F+"')";
cmd.ExecuteNonQuery();
}
elseif(temp1==temp2&&temp3==temp4&&temp2==temp3)
{
cmd.CommandText="insertintocard(telenumber,sort,sta)values('"+str+"','"+B+"','"+F+"')";
cmd.ExecuteNonQuery();
}
elseif(temp4==6||temp4==8)
{
cmd.CommandText="insertintocard(telenumber,sort,sta)values('"+str+"','"+C+"','"+F+"')";
cmd.ExecuteNonQuery();
}
else
{
cmd.CommandText="insertintocard(telenumber,sort,sta)values('"+str+"','"+D+"','"+F+"')";
cmd.ExecuteNonQuery();
}
}
conn.Close();
MessageBox.Show("号码生成成功!
","提示信息!
",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("号码生成失败!
","提示信息!
",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
privatevoiddataGridView1_CellContentClick(objectsender,DataGridViewCellEventArgse)
{
}
privatevoidtextBox1_TextChanged(objectsender,EventArgse)
{
}
privatevoidbutton4_Click(objectsender,EventArgse)
{
SqlConnectionconn=newSqlConnection("server=.;database=手机号码生成系统;uid=sa;pwd=;");
SqlDataAdaptersda=newSqlDataAdapter("select*fromclient",conn);
DataTabledt=newDataTable();
sda.Fill(dt);
BindingSourcebs=newBindingSource();
bs.DataSource=dt;
dataGridView1.DataSource=bs;
}
}
}
3、客户信息程序编写
①客户信息窗口界面
②客户信息代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceWindowsApplication3
{
publicpartialclassForm3:
Form
{
publicForm3()
{
InitializeComponent();
}
publicForm3(stringstrlable3Text)
{
InitializeComponent();
this.label7.Text=strlable3Text;
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
Close();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
//添加客户信息
SqlConnectionconn=newSqlConnection("server=.;database=手机号码生成系统;uid=sa;pwd=;");
conn.Open();
stringT="已出售";
SqlCommandcmd=conn.CreateCommand();
if(MessageBox.Show("确定出售?
","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
{
cmd.CommandText="insertintoclient(telenumber,name,sex,address,IDcard)values('"+label7.Text.Trim()+"','"+textBox1.Text.Trim()+"','"+textBox2.Text.Trim()+"','"+textBox3.Text.Trim()+"','"+textBox4.Text.Trim()+"')";
cmd.ExecuteNonQuery();
cmd.CommandText="updatecardsetsta='"+T+"'wheretelenumber='"+label7.Text.Trim()+"'";
cmd.ExecuteNonQuery();
this.Close();
}
else
{
this.Close();
}
conn.Close();
}
privatevoidForm3_Load(objectsender,EventArgse)
{
}
}
}
四、【实验总结】
通过这次实验,我掌握了很多以前没有掌握的知识,不仅更加深入了解了SqlServer的一些知识和各种语句的使用,也同时学习了C#编程的技巧,虽然这次实验花了很多时间来做,但是这次综合实验使我在各方面都有所提高。