?? courseeditform.cs
字號:
?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MySchoolPrj.Entity;
using MySchoolPrj.DAO;
namespace MySchoolPrj.CourseInfo
{
public partial class CourseEditForm : Form
{
public CourseEditForm()
{
InitializeComponent();
}
public bool flag=true;
//State枚舉
private State state;
public State State
{
get { return state; }
set { state = value; }
}
//Course類
private course cour;
public course Cour
{
get { return cour; }
set { cour = value; }
}
//增加
//把學生信息提交到數據庫
public void addCourse(course s)
{
try
{
//連接數據庫
//打開數據庫
DBHelper.con.Open();
//創建Command對象
SqlCommand cmd = new SqlCommand();
cmd.Connection = DBHelper.con;
cmd.CommandType = CommandType.Text;
//增加用的SqL語句
string sql = string.Format("insert into course values('{0}','{1}','{2}')", s.CourseId, s.CourseName, s.Memo);
cmd.CommandText = sql;
//執行命令
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
//操作出現異常
MessageBox.Show(ex.Message);
}
finally
{
//關閉數據庫
DBHelper.con.Close();
}
}
//獲取界面數據
public course getInteface()
{
course s = new course();
s.CourseId = txtCourseId.Text.Trim();
s.CourseName = txtCourseName.Text.Trim();
s.Memo = txtCourseMemo.Text.Trim();
return s;
}
//修改課程信息
public void UpdateTea(course s)
{
try
{
DBHelper.con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = DBHelper.con;
cmd.CommandType = CommandType.Text;
//修改用的Sqly語句
string sql = string.Format("update course set courseId='{0}',courseName='{1}'," +
"Memo='{2}'where courseId='{3}'", s.CourseId, s.CourseName, s.Memo, cour.CourseId);
cmd.CommandText = sql;
cmd.ExecuteNonQuery();//執行命令
txtCourseName.Focus();//焦點放在CourseName中
}
catch (Exception ex)
{// 錯誤操作
MessageBox.Show(ex.Message);
}
finally
{
//關閉數據庫
DBHelper.con.Close();
}
}
//把對象的數據顯示到界面
public void showData()
{
txtCourseId.Text = cour.CourseId;
txtCourseName.Text = cour.CourseName;
txtCourseMemo.Text = cour.Memo;
}
//增加窗體校驗
public bool validCourseisNull()
{
if (txtCourseName.Text == "")
{
MessageBox.Show("課程名不能為空");
return false;
}
else
{
return true;
}
}
//保存事件
private void btnOk_Click(object sender, EventArgs e)
{
if (flag)
{
if (state == State.dsAdd)
{
if (validCourseisNull())
{
addCourse(getInteface());
}
else { return; }
}
else
{
UpdateTea(getInteface());
}
this.Close();
}
else {
MessageBox.Show("出錯了!看到有提示還點確定!");
}
}
//重置 事件
//清空界面
private void btnReset_Click(object sender, EventArgs e)
{
if (state == State.dsAdd)
{
txtCourseId.Text = "";
txtCourseName.Text = "";
txtCourseMemo.Text = "";
label5.Text = "";
}
else
{//修改時CourseId不重置
txtCourseName.Text = "";
txtCourseMemo.Text = "";
label5.Text = "";
}
}
//退出事件
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
//增加時CourseId不能重復
//在Txtbox 后加上提示
private void txtCourseId_Leave(object sender, EventArgs e)
{
string s = txtCourseId.Text;
if ("".Equals(s))
{
label5.Text = "課程編號不能為空!";
flag = false;
return;
}
if (s.Length != 4)
{
label5.Text = "編號必須為4位,例如C008";
flag = false;
return;
}
if (s[0] != 'C')
{
label5.Text = "課程編號首字母必須為C";
flag = false;
return;
}
int i = 1;
for (i = 1; i < s.Length; i++)
{
if (s[i] < '0' || s[i] > '9')
{
label5.Text = "課程編號剩余3位必須為純數字";
flag = false;
return;
}
}
if (state == State.dsAdd)
{
// 獲取界面CourseId數據
string id = txtCourseId.Text;
//從數據庫提取數據
SqlCommand cmd = new SqlCommand();
cmd.Connection = DBHelper.con;
cmd.CommandType = CommandType.Text;
//查詢數據庫是否有相同的課程編號SQL語句
string sql = string.Format("select courseId from course where courseId='{0}'", id);
cmd.CommandText = sql;
DBHelper.con.Open();
SqlDataReader dataRaeader = cmd.ExecuteReader();//查詢查詢用戶命令
dataRaeader.Read();
//用HasRows屬性來判斷查詢結果是否存在
if (dataRaeader.HasRows)
{
label5.Text = "該Id已存在,重新輸入";
txtCourseId.Focus();//放置焦點
//選定文本框中所有對象
txtCourseId.SelectAll();
flag = false;
}
else
{
label5.Text = "該Id可以使用";
flag = true;
}
//關閉數據
dataRaeader.Close();
DBHelper.con.Close();
}
}
//修改時用的窗體初始化
private void CourseEditForm_Load_1(object sender, EventArgs e)
{
if (state == State.dsUpdate)
{
//關閉該控件
txtCourseId.Enabled = false;
//把對象的數據顯示到界面
showData();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -