插入数据前先判断表1表2里是不是有对应sno值的数据呗

解决方案 »

  1.   

    insert into 表1(sno) select sno from 表2 where  条件
      

  2.   

            string strsno = TextBox3.Text.Trim();
            string strkno = DropDownList4.SelectedValue.Trim();
            string strcredit = DropDownList5.SelectedValue.Trim();
            string strc = TextBox7.Text.Trim();
            try
            {
                string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
                myCon = new SqlConnection(myConStr);
                myCon.Open();
                myCom = new SqlCommand();
                myCom.Connection = myCon;
                string strSelect = "select * from sc where sno='{0}'and kno='{1}'";
                string sql = string.Format(strSelect, strsno, strkno);
                myCom.CommandType = CommandType.Text;
                myCom.CommandText = sql;
                myDa = new SqlDataAdapter();
                myDa.SelectCommand = myCom;
                myDs = new DataSet();
                myDa.Fill(myDs, "DS_MMB");
               if (myDs.Tables["DS_MMB"].Rows.Count <= 0)
                {
                    myCon = new SqlConnection(myConStr);
                    myCon.Open();
                    myCom = new SqlCommand();
                    myCom.Connection = myCon;
                    string strInsert = "insert into sc(sno,kno,credit,c) values('{0}','{1}','{2}','{3}')";
                    sql = string.Format(strInsert, strsno, strkno, strcredit, strc);
                    myCom.CommandType = CommandType.Text;
                    myCom.CommandText = sql;
                    myCom.ExecuteNonQuery();
                    Response.Write("<script language=javascript>alert('数据添加成功!')</script>");
                     }
                else
                {
                 Label3.Text = "信息已经存在";
                }
      

  3.   

    这是我的代码,这个是不可以重复添加的,但是我该怎么弄个添加学号时,必须是student表里有的。
    表1名字是score  表2名字是student
      

  4.   

    像这种情况,学号那里就不能用文本框了吧,用个下拉列表,从表student里面把学号读出来,然后选择哪个添加哪个,这样就不用判断学号是否重复添加了,,,只判断数据是否重复就可以了