根据另一列中包含的文本保存数据

时间:2018-07-09 07:54:22

标签: c# sql database

SQL1部分工作正常,但是我需要帮助弄清楚我在SQL2、3和4中的语句是否正确,因为它当前不起作用。我想在VIDEOPATH中出现“ Camera 1”一词时在VIEW中插入“ Audience View 1”,如果在VIDEOPATH中出现“ Camera 2”一词,则在VIEW中插入“ Audience View 2”。

        string[] files = Directory.GetFiles("C:/Users/sit/Videos/Done/");
        string view1 = "Audience View 1";
        string view2 = "Audience View 2";
        string view3 = "Lecturer View";

        foreach (string file in files) {

            string SQL1 = "INSERT INTO TBL_LESSONCAM(VIDEOPATH)VALUES('" + (file) + "')";
            string SQL2 = "UPDATE TBL_LESSONCAM(VIEW)VALUES('" + (view1) + "')" + "WHERE VIDEOPATH LIKE '%Camera 1%'";
            string SQL3 = "UPDATE TBL_LESSONCAM(VIEW)VALUES('" + (view2) + "')" + "WHERE VIDEOPATH LIKE '%Camera 2%'";
            string SQL4 = "UPDATE TBL_LESSONCAM(VIEW)VALUES('" + (view3) + "')" + "WHERE VIDEOPATH LIKE '%Camera 3%'";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = SQL1;
            cmd.CommandText = SQL2;
            cmd.CommandText = SQL3;
            cmd.CommandText = SQL4;
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();

        }

更新:我运行多个SQL语句的结构正确吗?

1 个答案:

答案 0 :(得分:0)

您的更新:

string SQL2 = "UPDATE TBL_LESSONCAM(VIEW)VALUES('" + (view1) + "')" + "WHERE VIDEOPATH LIKE '%Camera 1%'";

应该是

string SQL2 = "UPDATE TBL_LESSONCAM set view = '" + (view1) + "'" + " WHERE VIDEOPATH LIKE '%Camera 1%'; ";

请看看What are good ways to prevent SQL injection?