您的位置:首页 > 其它

通过insert型触发器对员工数据进行添加操作

2013-03-25 00:40 351 查看
View Code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.loadGridView1();
this.loadGridView2();
}
}
protected void loadGridView1()
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);

try
{
con.Open();
string sql = "select EmpID,EmpName,Number,Phone from employeeInfo03";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "employeeInfo");
GridView3.DataSource = ds;
GridView3.DataBind();

}
catch (Exception error)
{
Response.Write(error.Message.ToString());
}
finally
{

con.Close();
}
}
protected void loadGridView2()
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);

try
{
con.Open();
string sql = "select employeeID,employeeName,employeePay from employeePay03";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "employeeInfo");
GridView4.DataSource = ds;
GridView4.DataBind();

}
catch (Exception error)
{
Response.Write(error.Message.ToString());
}
finally
{

con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
string str = "select count(*) from employeeInfo03 where EmpID='" + TextID.Text.ToString() + "'";
SqlCommand mycom = new SqlCommand(str, con);
int intcont = Convert.ToInt32(mycom.ExecuteScalar());
if (intcont > 0)
{
Response.Write("<script language=javascript>alert('对不起!该编号已经存在!')</script>");
return;
}
else
{
string sql = "insert into employeeInfo03(EmpID,EmpName,Number,Phone)values('" + TextID.Text + "','" + TextName.Text + "','" + TextNumber.Text + "','" + Textphone.Text + "')";
try
{
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('插入成功')</script>");
}
catch (Exception error)
{
Response.Write(error.Message.ToString());
}
finally
{
con.Close();  //断开连接
loadGridView1();  //在GridView1里显示员工信息的方法;
loadGridView2();  //在GridView2里显示员工工资的方法;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐