| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 534 人关注过本帖
标题:[求助]怎样中断联系啊
只看楼主 加入收藏
hl0071
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2005-11-11
收藏
 问题点数:0 回复次数:3 
[求助]怎样中断联系啊

这两天在做一个选课系统的程序用SQL SERVER,在数据库里面有一个 " 判断是否已经选课" 的字段,在用户注册时就为之赋值为 "1",在学生选课后,会为之赋值 "2",然后在声明一个方法exit()来判断 "判断是否已经选课" 字段是否为"1", 来判断用户是否已经选课,选过课的就不能再选了,但是好像在学生选课后,方法好像检测到字段发生了改变,所以无论选没选过课,都不能再选了,不知道怎么样才能在选课让方法检测不到字段的改变,下面是我的代码,请高手帮忙啊,



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data ;
using System.Data .SqlClient ;

namespace WindowsApplication2
{
/// <summary>
/// Form5 的摘要说明。
/// </summary>
public class Form5 : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox checkBox4;
private System.Windows.Forms.CheckBox checkBox5;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public int a=0;

private string resource()
{

string strbox="";
if(checkBox1.Checked )
{
a=a+1;
strbox+=checkBox1.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox2.Checked )
{
a=a+1;
strbox+=checkBox2.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox3.Checked )
{
a=a+1;
strbox+=checkBox3.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox4.Checked )
{
a=a+1;
strbox+=checkBox4.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox5.Checked )
{
a=a+1;
strbox+=checkBox5.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}

return strbox;

}
private bool exit()
{
SqlCommand cmd;
SqlDataReader dr;
Form1 form1=new Form1 ();
string strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";
string str;
str="select * from 注册信息 where 帐号='"+form1.Userid +"'";
cmd=new SqlCommand();
cmd.Connection =new SqlConnection (strsql);
cmd.Connection .Open ();
cmd.CommandText =str;
dr=cmd.ExecuteReader();
dr.Read();

if(dr["判断是否已经选课"].ToString ()=="1")
{
return true;
cmd.Connection.Close ();
dr.Close ();
}
else
{
return false;
}


}


/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form5()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox5 = new System.Windows.Forms.CheckBox();
this.checkBox4 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(40, 24);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "网页设计";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(40, 72);
this.checkBox2.Name = "checkBox2";
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "ADCsee";
//
// checkBox3
//
this.checkBox3.Location = new System.Drawing.Point(40, 120);
this.checkBox3.Name = "checkBox3";
this.checkBox3.TabIndex = 2;
this.checkBox3.Text = "Flash";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.checkBox5);
this.groupBox1.Controls.Add(this.checkBox4);
this.groupBox1.Controls.Add(this.checkBox3);
this.groupBox1.Controls.Add(this.checkBox2);
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Location = new System.Drawing.Point(112, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(216, 264);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "请选择课程";
//
// checkBox5
//
this.checkBox5.Location = new System.Drawing.Point(40, 208);
this.checkBox5.Name = "checkBox5";
this.checkBox5.TabIndex = 4;
this.checkBox5.Text = "3Dmax";
//
// checkBox4
//
this.checkBox4.Location = new System.Drawing.Point(40, 160);
this.checkBox4.Name = "checkBox4";
this.checkBox4.TabIndex = 3;
this.checkBox4.Text = "photoshop";
//
// label1
//
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(344, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 72);
this.label1.TabIndex = 4;
this.label1.Text = "注意!每位学生最多只能选择3门!!";
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(264, 320);
this.button2.Name = "button2";
this.button2.TabIndex = 6;
this.button2.Text = "退出";
//
// Form5
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 366);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.groupBox1);
this.Name = "Form5";
this.Text = "选课";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form5_Closing);
this.Load += new System.EventHandler(this.Form5_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
if(exit()==true)
{
SqlCommand cmd;
try
{


string strsql;
string str;
int row;
Form1 form1=new Form1 ();
strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";

string str1 =resource();
if (a==0||a>3)
{
MessageBox.Show ("每位同学最多只能选三门课程","提示");
a=0;
return;
}
else
{

str="update 注册信息 set\n";
str+="所选择的课程='"+str1+"\n'";
str+="where 帐号='"+form1.Userid+"'" ;
}
cmd=new SqlCommand ( );
cmd.Connection =new SqlConnection (strsql);
cmd.Connection .Open ();
cmd.CommandText =str;
row=cmd.ExecuteNonQuery ();
if (row==1)
{
MessageBox.Show ("成功");
cmd.Connection .Close ();

}
else
{
MessageBox.Show ("失败");
return ;
}
}


catch(Exception ex)
{
MessageBox.Show (ex.Message );
}
}
else
{
MessageBox.Show ("你已经选过课程,不能再更改!","提示");
}
}

private void Form5_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
SqlCommand cmd;
string strsql;
string str;
Form1 form1=new Form1 ();
strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";
str="update 注册信息 set\n";
str+="判断是否已经选课='"+2+"\n'";
str+="where 帐号='"+form1.Userid+"'" ;
cmd=new SqlCommand ( );
cmd.Connection =new SqlConnection (strsql);
cmd.Connection .Open ();
cmd.CommandText =str;
cmd.ExecuteNonQuery ();
cmd.Connection .Close ();

}

private void Form5_Load(object sender, System.EventArgs e)
{

}
}
}

搜索更多相关主题的帖子: 联系 
2006-06-29 16:16
houyunqing
Rank: 1
等 级:新手上路
帖 子:476
专家分:0
注 册:2005-4-1
收藏
得分:0 
这么长的代码看了会死人的...好象也不是很难看哦,大概明白你的意思了

我没怎么仔细看,但是我知道你写的代码必须改

把"是否已经选课"改成"已选科目数"

在判断能否继续选课的时候,只需要判断"已选科目数"是否等于或者大于3

说实话,你这程序真的很不完善啊....不过看了你这个简单的程序后我对MS的数据库系统信心真的增强了很多...(哈哈哈)

寻求挑战,追求完美 Oh,my god!
2006-07-01 09:29
hl0071
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2005-11-11
收藏
得分:0 
靠,不是吧

一切都是快乐的
2006-07-01 09:42
houyunqing
Rank: 1
等 级:新手上路
帖 子:476
专家分:0
注 册:2005-4-1
收藏
得分:0 
另外呢,还可以给你点建议,在自己这里使用一个Student类,来记录学生的各项信息,包括选课情况,最后在用户离开的时候再同服务器联系将信息完成修改,以此来避免过多的不必要的操作,这会让你的程序显得有条理得多

寻求挑战,追求完美 Oh,my god!
2006-07-01 10:03
快速回复:[求助]怎样中断联系啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031208 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved