跪求c#期末课程设计,绘图板或者学生管理系统,谢谢!!!
发布网友
发布时间:2022-05-29 17:31
我来回答
共2个回答
热心网友
时间:2023-10-28 22:42
public bool CheckNull() //判断各个文本框是否为空
{
control = this;
for( int i = 0; i < control.Controls.Count; i++ )
{
if( control.Controls[i].Enabled
&&((control.Controls[i] is TextBox)
||(control.Controls[i] is ComboBox))
&& (control.Controls[i].Text.Trim() == "")
&& (control.Controls[i].Tag != null) )
{
if( control.Controls[i] is TextBox && ((TextBox)control.Controls[i]).ReadOnly)
{
continue;
}
MessageBox.Show((String)(control.Controls[i].Tag) + "不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
control.Controls[i].Text = "";
control.Controls[i].Focus();
return false;
}
}
return true;
}
//清除文本框内容
public void ClearContent()
{
control = this;
for( int i = 0; i < control.Controls.Count; i++ )
{
if( ( control.Controls[i] is TextBox ))
(control.Controls[i] as TextBox ).Text = "";
else if( control.Controls[i] is ComboBox )
( control.Controls[i] as ComboBox ).SelectedIndex = -1 ;
else if( control.Controls[i] is CheckBox )
( control.Controls[i] as CheckBox ).Checked = false;
}
}
private void SetButtonStatus( bool flag )//设置“添加”“保存”按钮的切换
{
btnAdd.Visible = flag;
btnSave.Visible = !flag;
}
private void SetControlsEnable(bool isEnable)//使控件变为可编辑(即可以输入文字)
{
control = this;
for( int i = 0; i < control.Controls.Count; i++)
{
if ( (control.Controls[i] is TextBox)
||(control.Controls[i] is ComboBox)
||(control.Controls[i] is PictureBox))
{
control.Controls[i].Enabled = isEnable;
}
}
}
public bool ExecuteSQL(String sql)
{
SqlConnection myConnection = new SqlConnection(ConnectionString); //连接数据库
String cmdText = sql;
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
try
{
myCommand.ExecuteNonQuery();
myConnection.Close();
return true;
}
catch (Exception ex)
{
myConnection.Close();
return false;
}
}
public DataSet GetDataSetBySql(String sql)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);//连接数据库
String cmdText = sql;
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);//DataAdapter的Fill方法用于使用DataAdapte的SelectCommand的结果来填充DataSet
myConnection.Open(); //打开数据库
DataSet ds = new DataSet(); //DataSet对象可以保存多个数据表DataTable的查询结果,
//DataSet不一定要连接到一个数据库上,它本身是一个离线数据,
//只有在对数据库进行编辑时才需要对数据库进行连接
da.Fill(ds);
return ds;
}
public void LoadStudent() //查找“学生”信息
{
sqlText = "select * from students";
dsStudent = GetDataSetBySql(sqlText);
dtStudent = dsStudent.Tables[0].Copy();
dtStudent.TableName = "student";
dgShowStudent.DataSource = dtStudent;
}
public void Loadserstu(String sqlstu )//输出查找的学生信息
{
String stext=sqlstu;
dsStudent = GetDataSetBySql(stext);
dtStudent = dsStudent.Tables[0].Copy();
dtStudent.TableName = "stu";
dgShowStudent.DataSource = dtStudent;
}
public void LoadDept()
{
String sqlText = "select * from Department";
dsDept = GetDataSetBySql(sqlText);
dtDept = dsDept.Tables[0].Copy();
// cmbDept.DataSource = dtDept;
foreach( DataRow row in dtDept.Rows )
cmbDept.Items.Add( row["Sdept"].ToString() );
}
private void CFrmInputStudent_Load(object sender, System.EventArgs e)
{
LoadStudent();
LoadDept();
SetControlsEnable(false);
btnSave.Enabled = true;
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
btndel.Enabled = false;
btndeled.Enabled = false;
btnup.Enabled = false;
btnuped.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
SetControlsEnable(true);
SetButtonStatus( false );
ClearContent();
}
private void btnSave_Click(object sender, System.EventArgs e)
{
if(!CheckNull())
return;
strNumber = this.tbNumber.Text.Trim();
strAge = this.tbAge.Text.Trim();
strName = this.tbName.Text.Trim();
strSex = cmbSex.SelectedItem.ToString();
strDept = cmbDept.SelectedItem.ToString();
sqlText = "insert into students (Sno,Sname,Ssex,Sage,Sdept) values ('" + strNumber + "','"
+ strName +"','"
+ strSex +"','"
+ strAge +"','"
+ strDept +"')";
bool tag = ExecuteSQL(sqlText);
if (!tag)
{
MessageBox.Show("插入失败!");
return;
}
btnsered.Enabled = true;
btnser.Enabled = true;
btndel.Enabled = true;
btndeled.Enabled = true;
btnup.Enabled = true;
btnuped.Enabled = true;
LoadStudent();
SetButtonStatus( true );
SetControlsEnable( false);
ClearContent();
}
private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close();
this.Dispose();
}
private void tbAge_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
String strAge = tbAge.Text;
for( int i = 0; i < strAge.Length; i++ )
{
if( strAge[i] >= '0' && strAge[i] <= '9')
continue;
else
{
MessageBox.Show( "代码必须是数字!","错误", MessageBoxButtons.OK, MessageBoxIcon.Error );
tbAge.SelectAll();
e.Cancel = true;
return;
}
}
}
private void btnser_Click(object sender, EventArgs e)
{
btndeled.Enabled = false;
btndel.Enabled = false;
btnup.Enabled = false;
btnuped.Enabled = false;
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Visible = false;
SetControlsEnable(true);
}
private void btnsered_Click(object sender, EventArgs e)
{
strNumber = this.tbNumber.Text.Trim();
sqlText = "select * from students where sno='" + strNumber + "' ";
bool tag = ExecuteSQL(sqlText);
if (!tag)
{
MessageBox.Show("没有相关信息");
return;
}
btnser.Visible = true;
btnAdd.Enabled = true;
btndel.Enabled = true;
btndeled.Enabled = true;
btnup.Enabled = true;
btnuped.Enabled = true;
btnser.Enabled = true;
btnsered.Enabled = true;
Loadserstu(sqlText);
SetControlsEnable(false);
ClearContent();
}
private void btndel_Click(object sender, EventArgs e)
{
btnup.Enabled = false;
btnuped.Enabled = false;
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
btndel.Visible = false;
tbNumber.Enabled = true;
}
private void btndeled_Click(object sender, EventArgs e)
{
strNumber = this.tbNumber.Text.Trim();
sqlText = "delete from students where sno='" + strNumber + "'";
MessageBox.Show("是否删除","注意!", MessageBoxButtons.YesNo);
bool tag = ExecuteSQL(sqlText);
if(!tag)
{
MessageBox.Show("删除出错!");
return;
}
LoadStudent();
ClearContent();
btnAdd.Enabled = true;
btnSave.Enabled = true;
btnser.Enabled = true;
btnsered.Enabled = true;
btndel.Visible=true;
tbNumber.Enabled = false;
btnup.Enabled = true;
btnuped.Enabled = true;
}
private void btnup_Click(object sender, EventArgs e)
{
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
btndel.Enabled = false;
btndeled.Enabled = false;
btnup.Visible = false;
SetControlsEnable(true);
}
private void btnuped_Click(object sender, EventArgs e)
{
if (!CheckNull())
return;
strNumber = tbNumber.Text.Trim();
strName = tbName.Text.Trim();
strSex = cmbSex.SelectedItem.ToString();
strAge = tbAge.Text.Trim();
strDept = cmbDept.SelectedItem.ToString();
sqlText = "update students set sname='" + strName + "', ssex='" + strSex + "', sage='" + strAge + "',sdept='" + strDept + "'where sno='" + strNumber + "';";
bool tag = ExecuteSQL(sqlText);
if(!tag)
{
MessageBox.Show("修改出错!");
return;
}
LoadStudent();
ClearContent();
SetControlsEnable(false);
btnAdd.Enabled=true;
btnSave.Enabled=true;
btnser.Enabled=true;
btnsered.Enabled=true;
btndel.Enabled=true;
btndeled.Enabled=true;
btnup.Visible=true;
}
}
}
学生管理系统 做的不全。。 BUG 存在很多。。。
热心网友
时间:2023-10-28 22:42
告诉邮箱才好,不然怎发你啊