📄 teachershangchuan.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
string myconn=System .Configuration .ConfigurationManager .AppSettings ["ConnectionString"];
conn = new SqlConnection(myconn );
teaID.Text = Session["userName"].ToString();
string sqlselect = "select teaID,teaName from teacher where teacher.teaID='" + Session["userName"].ToString() + " '";
SqlCommand comm1 = new SqlCommand(sqlselect, conn);
conn.Open();
SqlDataReader dr = comm1.ExecuteReader();
if (dr.Read())
{
teaName.Text = dr["teaName"].ToString();
teaID.Text = dr["teaID"].ToString();
}
conn.Close();
if (Session["userName"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
if (!IsPostBack)
GridViewBind();
}
}
private void GridViewBind()
{
string SqlStr = "SELECT Cource.*,Teacher.*,type.* FROM Cource,Teacher,type where Cource.teaID=Teacher.teaID and Cource.typeID=type.typeID and teacher.teaid='" + Session["userName"].ToString ()+ "'";
DataSet ds = new DataSet();
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
if (conn.State.ToString() == "Open")
conn.Close();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //GridView编辑项索引等于单击行的索引
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出要删除记录的主键值
string teaID = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
//string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString; //取出连接字符串
string SqlStr = "delete from Cource where courceID='" + id + "'and teaID='" + teaID + "'";
string Sqlstr1 = "delete from Elect where courceID='" + id + "'and teaID='" + teaID + "'";
try
{
// SqlConnection conn = new SqlConnection(connStr); //创建连接对象
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
SqlCommand comm1 = new SqlCommand(Sqlstr1, conn);
comm.ExecuteNonQuery(); //执行删除
comm.Dispose();
comm1.ExecuteNonQuery();
comm1.Dispose();
if (conn.State.ToString() == "Open")
conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
//添加课程事件
protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
{
if (txtCourceID.Text == "")
{
Response.Write("请输入课程号!");
txtCourceID.Focus();
return;
}
string sql = "insert into Cource(courceID,typeID,teaID,courceName,courceTime,courceAddress,xueshi,credit,courcerenshu,departid,courceInfo) values('" + txtCourceID.Text + "','" + ddltypeName.SelectedValue.ToString() + "','" + Session["userName"].ToString() + "','" + txtCourceName.Text + "','" + txtTime.Text + "','" + txtAddress.Text + "','" + xueshi.Text + "','" + credit.Text + "','" + courcerenshu.Text + "','" + ddldepart.SelectedValue.ToString() + "','" + txtCourceInformation.Text + "')";
SqlCommand insertCmd = new SqlCommand(sql, conn);
try
{
conn.Open();
int flag = insertCmd.ExecuteNonQuery(); //执行添加
if (flag > 0) //如果添加成功
{
lblMessage.Text = "成功添加课程信息!";
}
else //如果添加失败
{
lblMessage.Text = "添加课程信息失败,查看输入是否正确!";
}
}
catch (System.Exception ee)
{
Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
GridViewBind();
}
}
protected void imgBtnReset_Click(object sender, ImageClickEventArgs e)
{
txtCourceID.Text = "";
txtCourceName.Text = "";
txtTime.Text = "";
txtAddress.Text = "";
txtCourceInformation.Text = "";
courcerenshu.Text = "";
credit.Text = "";
xueshi.Text = "";
txtCourceID.Focus();
}
protected void Button4_Click(object sender, EventArgs e)
{
Response.Redirect("teacherQueryElect.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -