SqlConnection conn = new SqlConnection(connstring);
        conn.Open();
        try
        {
            string sqlstr = "select * from K_LF  where lf_bm='" + BM + "' and LF_DM='" + JJDM + "' AND LF_RQ LIKE '" + RQ + "%'";
            SqlCommand CMDselect = new SqlCommand(sqlstr, conn);
            SqlDataReader RA = CMDselect.ExecuteReader();
            string updatestr = "";
            if (RA.Read())
                updatestr = "update K_LF set lf_rq='" + RQ + "' where lf_bm='" + BM + "' and LF_DM='" + JJDM + "'";
            else
                updatestr = "insert into K_LF values ('" + BM + "','" + JJDM + "','" + JJMC + "','" + RQ + "')";
            SqlCommand CMD = new SqlCommand(updatestr, conn);
            CMD.ExecuteNonQuery();            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            conn.Close();
        }

解决方案 »

  1.   

    不報錯,就是不執行我的sql代碼。
      

  2.   

    console.writeline(updatestr)      ' 注:VB.NET语法把这个帖出来看一下
      

  3.   

    語法沒有錯,就是我故意把sql語法寫錯它也不報錯。
      

  4.   

    TO:不報錯,就是不執行我的sql代碼。什么叫不执行sql代码啊?楼主没有把问题说清楚啊..断点调试一下看看是怎么回事?
      

  5.   

    SqlCommand CMD = new SqlCommand(updatestr, conn);
                CMD.ExecuteNonQuery();
    到這裡應該是要更新或插入到數據庫,但在數據庫的數據沒有變化
    斷點調試了,執行是沒有問題,語法也沒問題。就是沒有更改數據庫。
      

  6.   

    SqlConnection conn = new SqlConnection(connstring);
            conn.Open();
    樓主的不報錯
    估計是到上面這兩行代碼就報錯了
      

  7.   

    可是程序它不報錯呀。
    RA.Close()了也不行。
      

  8.   

    下面是全碼
     [WebMethod
        public bool saveHJ(string BM, string RQ, string JJDM, string JJMC)
        {
            SqlConnection conn = new SqlConnection(connstring);
            conn.Open();
            try
            {
                string sqlstr = "select * from K_LF  where lf_bm='" + BM + "' and LF_DM='" + JJDM + "' AND LF_RQ LIKE '" + RQ + "%'";
                SqlCommand CMDselect = new SqlCommand(sqlstr, conn);
                SqlDataReader RA = CMDselect.ExecuteReader();
                string updatestr = "";
                if (RA.Read())
                    updatestr = "update K_LF set lf_rq='" + RQ + "' where lf_bm='" + BM + "' and LF_DM='" + JJDM + "'";
                else
                    updatestr = "insert into K_LF values ('" + BM + "','" + JJDM + "','" + JJMC + "','" + RQ + "')";
                SqlCommand CMD = new SqlCommand(updatestr, conn);
                CMD.ExecuteNonQuery();
                RA.Close();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
      

  9.   

    捕捉个异常看看阿catch(Exception ex)
    {
       ...
    }
      

  10.   

    找到了改成下面的就好了
    [WebMethod
        public bool saveHJ(string BM, string RQ, string JJDM, string JJMC)
        {
            SqlConnection conn = new SqlConnection(connstring);
            conn.Open();
            try
            {
                string sqlstr = "select * from K_LF  where lf_bm='" + BM + "' and LF_DM='" + JJDM + "' AND LF_RQ LIKE '" + RQ + "%'";
                SqlCommand CMDselect = new SqlCommand(sqlstr, conn);
                SqlDataReader RA = CMDselect.ExecuteReader();
                string updatestr = "";
                if (RA.Read())
                    updatestr = "update K_LF set lf_rq='" + RQ + "' where lf_bm='" + BM + "' and LF_DM='" + JJDM + "'";
                else
                    updatestr = "insert into K_LF values ('" + BM + "','" + JJDM + "','" + JJMC + "','" + RQ + "')";
                RA.Close();
                SqlCommand CMD = new SqlCommand(updatestr, conn);
                CMD.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }