各位大哥大姐们帮个忙啊,我遇到重复插入数据的问题,帮忙解决一下.
代码如下
public partial class images_St_assigncourse : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{//连接到数据库
SqlConnection con0 = DB.CreateConn();
//con0.Open();
if (!IsPostBack)
{
//绑定教师下拉列表
con0.Open();
string st_sql = "select * from Teacher";
SqlCommand st_comm0 = new SqlCommand(st_sql, con0);
SqlDataReader st_dr0 = st_comm0.ExecuteReader();
while (st_dr0.Read())
{
Ddl_teacher.Items.Add(new ListItem(st_dr0["Teacher_name"].ToString(), st_dr0["Teacher_id"].ToString()));
}
con0.Close();
//课程名称下拉列表框绑定
con0.Open();
string st_sql1 = "select * from Course";
//string st_sql1 = "select * from Course where Course.Course_kind='"+Ddl_coursekind.SelectedValue+"'";
SqlCommand st_comm1 = new SqlCommand(st_sql1, con0);
SqlDataReader st_dr1 = st_comm1.ExecuteReader();
while (st_dr1.Read())
{
Ddl_course.Items.Add(new ListItem(st_dr1["Course_name"].ToString(), st_dr1["Course_id"].ToString()));
}
con0.Close();
//班级名称下拉列表框绑定
con0.Open();
string st_sql2 = "select * from Class";
SqlCommand st_comm2 = new SqlCommand(st_sql2, con0);
SqlDataReader st_dr2 = st_comm2.ExecuteReader();
Ddl_class.Items.Add(new ListItem("", ""));
while (st_dr2.Read())
{
Ddl_class.Items.Add(new ListItem(st_dr2["Class_name"].ToString(), st_dr2["Class_id"].ToString()));
}
con0.Close();
}
}
//检查在表中是否存在与要插入的数据相同的
//public bool CheckData()
//{
// SqlConnection con = DB.CreateConn();//用类DB连接数据库
// con.Open();//打开数据库
// string st_sql = "select * from Teacher_course where Teacher_course.Teacher_id='" + Ddl_teacher.SelectedValue + "' and Teacher_course.Course_id='"+Ddl_course.SelectedValue+"'";
// SqlCommand st_cmd = new SqlCommand(st_sql,con);
// st_cmd.Parameters.Add("@Teach_id",SqlDbType.VarChar,50);
// st_cmd.Parameters["Teach_id"].Value = Ddl_teacher.SelectedValue;
// st_cmd.Parameters.Add("@Course_id", SqlDbType.VarChar, 50);
// st_cmd.Parameters["Course_id"].Value = Ddl_course.SelectedValue;
// SqlDataReader sdr = st_cmd.ExecuteReader();
// if(sdr.Read())
// return true;
//}
protected void Btn_assign_Click(object sender, EventArgs e)
{
SqlConnection conn = DB.CreateConn();
conn.Open();
//if(CheckData())
//{
//插入语句
string st_sqlstr = "insert into Teacher_course(Teacher_id,Class_id,Course_year,Course_id) values(@Teach_id,@Clas_id,@Cours_year,@Cours_id)";
//删除语句
st_comm.ExecuteNonQuery();
st_comm = new SqlCommand(st_sqlstr, conn);
st_comm.Parameters.Add(new SqlParameter("@Teach_id", SqlDbType.VarChar, 50));
st_comm.Parameters["@Teach_id"].Value = Ddl_teacher.SelectedItem.Value;
st_comm.Parameters.Add(new SqlParameter("@Cours_id", SqlDbType.VarChar, 50));
st_comm.Parameters["@Cours_id"].Value = Ddl_course.SelectedItem.Value;
st_comm.Parameters.Add(new SqlParameter("@Clas_id", SqlDbType.VarChar, 50));
st_comm.Parameters["@Clas_id"].Value = Ddl_class.SelectedItem.Value;
st_comm.Parameters.Add(new SqlParameter("@Cours_year", SqlDbType.VarChar, 50));
st_comm.Parameters["@Cours_year"].Value = Tbx_year.Text;
try
{
st_comm.ExecuteNonQuery();
Lbl_note.Text = "添加成功,请返回";
Lbl_note.Style["color"]="green";
}
catch (SqlException)
//else
{
Lbl_note.Text = "添加失败";
Lbl_note.Style["color"] = "red";
}
conn.Close();
}
}
我想用一个CheckData函数来判断在表中是否存在与将要插入的数据相同的数据,怎么写啊?