Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Umbraco C#Controller中SQL Server查询未正确执行_C#_.net_Angularjs_Sql Server_Umbraco7 - Fatal编程技术网

在Umbraco C#Controller中SQL Server查询未正确执行

在Umbraco C#Controller中SQL Server查询未正确执行,c#,.net,angularjs,sql-server,umbraco7,C#,.net,Angularjs,Sql Server,Umbraco7,因此,我的代码似乎运行良好,没有抛出异常或错误,但是当我在“import complete”警报跳转后检查我的db表时,没有任何异常或错误 请注意: 我指的是SaveLT函数中的查询 这是我的C#控制器: using UmbracoImportExportPlugin.Models; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using

因此,我的代码似乎运行良好,没有抛出异常或错误,但是当我在“import complete”警报跳转后检查我的db表时,没有任何异常或错误

请注意: 我指的是SaveLT函数中的查询 这是我的C#控制器:

using UmbracoImportExportPlugin.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Umbraco.Core.Persistence;
using Umbraco.Web;
using Umbraco.Web.WebApi;

namespace UmbracoImportExportPlugin.App_Code
{

    public class ImportNewDictionaryController : UmbracoAuthorizedApiController
    {
        public string basePath;

        //Locate specific path
        public void LocatePath()
        {
            this.basePath = System.Web.Hosting.HostingEnvironment.MapPath(@"/upload");
        }
        [System.Web.Http.AcceptVerbs("GET", "POST")]
        //[System.Web.Http.HttpPost]
        public void SaveFile()
        {
            var myContext = Request.TryGetHttpContext();
            List<string> keys = new List<string>();
            if (myContext.Success)

            {
                HttpPostedFileBase myFile = myContext.Result.Request.Files["file"];
                if (myFile == null)
                {
                   throw new HttpException("invalid file");
                }
                else
                {
                    StreamReader csvreader = new StreamReader(myFile.InputStream);


                    while (!csvreader.EndOfStream)
                    {
                        var line = csvreader.ReadLine();
                        if (line != "Key")
                        keys.Add(line);
                    }
                }
                UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
                var remove = new Sql("DELETE FROM cmsDictionary");
                int rem = db.Execute(remove);

                foreach (string item in keys)
                {
                    var insert = new Sql("INSERT INTO cmsDictionary VALUES (NEWID(), null,'" + item + "')");
                    int res = db.Execute(insert);
                }
            }
        }

        [System.Web.Http.AcceptVerbs("GET", "POST")]
        public void SaveLT()
        {
            List<string> id = new List<string>();
            var myContext = Request.TryGetHttpContext();
            List<string> data = new List<string>();
            if (myContext.Success)
            {
                HttpPostedFileBase myFile = myContext.Result.Request.Files["file"];
                if (myFile == null)
                {
                    throw new HttpException("invalid file");
                }
                else
                {
                    StreamReader csvreader = new StreamReader(myFile.InputStream);


                    while (!csvreader.EndOfStream)
                    {
                        var line = csvreader.ReadLine();
                        if (line != "Value")
                            data.Add(line);
                    }
                }
                UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
                var remove = new Sql("DELETE FROM cmsLanguageText");
                int rem = db.Execute(remove);
                for (var i = 1; i < 142; i++ )
                {
                     foreach (string lang in data)
                    {
                        foreach (string ident in id)
                        {
                            Int32.Parse(ident);
                            var insertNew = new Sql("INSERT INTO cmsLanguageText (languageId, UniqueId, value) VALUES (" + ident + " , NEWID() , '" + lang + "')");
                            int res = db.Execute(insertNew);
                        }
                    }
                }
            }
        }
        public List<int> getList()
        {
            UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
            var select = new Sql("SELECT [id] FROM umbracoLanguage;");
            List<int> id = new List<int>();
            id = db.Fetch<int>(select);
            return id;
        }

        public String GetUserName()
        {
            var current = UmbracoContext.Current;
            var user = current.UmbracoUser;
            return user.Name.ToString();
        }
    }
}
angular.module("umbraco")
    .controller("ILTController", function ($scope, $http) {
        $scope.fileUpload = {};
        $scope.uploadLanguage = function () {
            var uploadUrl = " /umbraco/backoffice/api/ImportNewDictionary/SaveLT";
            var fd = new FormData();

            fd.append('file', $scope.fileUpload);

            $http.post(uploadUrl, fd, {
                transformRequest: angular.identity,
                headers: { 'Content-Type': undefined }
            })
            .success(function (data) {
                // ok
                alert("Import Complete!");
            })
            .error(function () {
                // handle upload error
                alert("Import Unsuccessful!");
            })
        };

    });

angular.module("umbraco").directive("qwSingleLanguageUpload", function () {
    return {
        restrict: "A",
        replace: false,
        scope: {
            myValue: '=qwSingleLanguageUpload'
        },
        link: function (scope, element, attr) {
            element.bind('change', function () {
                scope.myValue = element[0].files[0];

                if (scope.$$phase) {
                    scope.$apply();
                }
            });
        }
    }
});
我更关心的是为什么它不将导入的内容(CSV文件)导入db表,而不是安全性,因为这是一个Umbraco backoffice插件,并且只有具有特定权限的特定用户才能访问“导入文件”选项卡。在进行SQL注入之前,他们必须访问后台,这是毫无意义的,因为此查询正在更改的任何内容都可以由仪表板上的用户更改

知道为什么我的数据库表可能无法上载吗?我已经设置了断点,foreach循环正在运行,因此我不确定现在该做什么。

您设置
List id=new List()
,然后执行
foreach(id中的字符串标识)
。由于
id
是一个空列表,您没有什么可迭代的,因此永远不会到达INSERT语句

尝试用一些要迭代的值填充
id

除非我在快速通读中误读了您的代码。

您设置
列表id=new List()
,然后对每个(id中的字符串标识)执行
操作。
。由于
id
是一个空列表,您没有什么可迭代的,因此永远不会到达INSERT语句

尝试用一些要迭代的值填充
id


除非我在快速通读中误读了您的代码。

而不是尝试SQL并手动将其插入数据库,Umbraco实际上有一个本地化服务和模型:

[System.Web.Http.AcceptVerbs("GET", "POST")]
    public void SaveLT()
    {
            var ls = ApplicationContext.Current.Services.LocalizationService;    

            //create a holder for the item's DictionaryTranslations

            List<DictionaryTranslation> _hello = new List<DictionaryTranslation>();
            List<DictionaryTranslation> _submit = new List<DictionaryTranslation>();
            List<DictionaryTranslation> _form = new List<DictionaryTranslation>();
            List<DictionaryTranslation> _bootstrap = new List<DictionaryTranslation>();
            List<DictionaryTranslation> _world = new List<DictionaryTranslation>();
            List<DictionaryTranslation> _heaven = new List<DictionaryTranslation>();
            List<DictionaryTranslation> _hell = new List<DictionaryTranslation>();
            List<DictionaryTranslation> _this = new List<DictionaryTranslation>();
            List<DictionaryTranslation> _sublime1 = new List<DictionaryTranslation>();

             //the constructor for a DictionaryItem requires the Umbraco language object and value of the translated text                       
             //so get the language object, eg from Iso Code                            
            var language = ls.GetLanguageByIsoCode("he-IL");
            var lang1 = ls.GetLanguageByIsoCode("ru");
            var lang2 = ls.GetLanguageByIsoCode("en-US");

            // here we create a french translation for our item and add it to the list    
            DictionaryTranslation hebhello = new DictionaryTranslation(language, "שלום");
            DictionaryTranslation rushello = new DictionaryTranslation(lang1, "Здравствуйте");
            DictionaryTranslation enghello = new DictionaryTranslation(lang2, "Blah");
             _hello.Add(hebhello);
             _hello.Add(rushello);
             _hello.Add(enghello);
             DictionaryTranslation hebsubmit = new DictionaryTranslation(language, "שלח");
             DictionaryTranslation russubmit = new DictionaryTranslation(lang1, "Отправить");
             DictionaryTranslation engsubmit = new DictionaryTranslation(lang2, "Submit");
             _submit.Add(hebsubmit);
             _submit.Add(russubmit);
             _submit.Add(engsubmit);
             DictionaryTranslation hebform = new DictionaryTranslation(language, "טופס");
             DictionaryTranslation rusform = new DictionaryTranslation(lang1, "форма");
             DictionaryTranslation engform = new DictionaryTranslation(lang2, "Form");
             _form.Add(hebform);
             _form.Add(rusform);
             _form.Add(engform);
             DictionaryTranslation hebbtstrp = new DictionaryTranslation(language, "אֹזֶן הַנַעַל");
             DictionaryTranslation rusbtstrp = new DictionaryTranslation(lang1, "начальная загрузка");
             DictionaryTranslation engbtstrp = new DictionaryTranslation(lang2, "Bootstrap");
             _bootstrap.Add(hebbtstrp);
             _bootstrap.Add(rusbtstrp);
             _bootstrap.Add(engbtstrp);
             DictionaryTranslation hebworld = new DictionaryTranslation(language, "עוֹלָם");
             DictionaryTranslation rusworld = new DictionaryTranslation(lang1, "Мир");
             DictionaryTranslation engworld = new DictionaryTranslation(lang2, "World");
             _world.Add(hebworld);
             _world.Add(rusworld);
             _world.Add(engworld);
             DictionaryTranslation hebheaven = new DictionaryTranslation(language, "גן העדן");
             DictionaryTranslation rusheaven = new DictionaryTranslation(lang1, "небо");
             DictionaryTranslation engheaven = new DictionaryTranslation(lang2, "Heaven");
             _heaven.Add(hebheaven);
             _heaven.Add(rusheaven);
             _heaven.Add(engheaven);
             DictionaryTranslation hebhell = new DictionaryTranslation(language, "גגֵיהִנוֹם");
             DictionaryTranslation rushell = new DictionaryTranslation(lang1, "ад");
             DictionaryTranslation enghell = new DictionaryTranslation(lang2, "Hell");
             _hell.Add(hebhell);
             _hell.Add(rushell);
             _hell.Add(enghell);
             DictionaryTranslation hebthis = new DictionaryTranslation(language, "זֶה");
             DictionaryTranslation rusthis = new DictionaryTranslation(lang1, "это");
             DictionaryTranslation engthis = new DictionaryTranslation(lang2, "This");
             _this.Add(hebthis);
             _this.Add(rusthis);
             _this.Add(engthis);
             DictionaryTranslation hebsub = new DictionaryTranslation(language, "נִשׂגָב");
             DictionaryTranslation russub = new DictionaryTranslation(lang1, "возвышенный");
             DictionaryTranslation engsub = new DictionaryTranslation(lang2, "Sublime");
             _sublime1.Add(hebsub);
             _sublime1.Add(russub);
             _sublime1.Add(engsub);

                 //get or create a DictionaryItem, (passing in the Dictionary Key)                           
            IDictionaryItem hello = ls.DictionaryItemExists("hello_button") ? ls.GetDictionaryItemByKey("hello_button") : new DictionaryItem("hello_button");
            IDictionaryItem submit = ls.DictionaryItemExists("submit_button") ? ls.GetDictionaryItemByKey("submit_button") : new DictionaryItem("submit_button");
            IDictionaryItem form = ls.DictionaryItemExists("form_button") ? ls.GetDictionaryItemByKey("form_button") : new DictionaryItem("form_button");
            IDictionaryItem btstrp = ls.DictionaryItemExists("bootstrap_button") ? ls.GetDictionaryItemByKey("bootstrap_button") : new DictionaryItem("bootstrap_button");
            IDictionaryItem world = ls.DictionaryItemExists("world_button") ? ls.GetDictionaryItemByKey("world_button") : new DictionaryItem("world_button");
            IDictionaryItem heaven = ls.DictionaryItemExists("heaven_button") ? ls.GetDictionaryItemByKey("heaven_button") : new DictionaryItem("heaven_button");
            IDictionaryItem hell = ls.DictionaryItemExists("hell_button") ? ls.GetDictionaryItemByKey("hell_button") : new DictionaryItem("hell_button");
            IDictionaryItem This = ls.DictionaryItemExists("this_button") ? ls.GetDictionaryItemByKey("this_button") : new DictionaryItem("this_button");
            IDictionaryItem sublime = ls.DictionaryItemExists("sublime_button") ? ls.GetDictionaryItemByKey("sublime_button") : new DictionaryItem("sublime_button");
             // set the translations created above    
            hello.Translations = _hello;
            submit.Translations = _submit;
            form.Translations = _form;
            btstrp.Translations = _bootstrap;
            world.Translations = _world;
            heaven.Translations = _heaven;
            hell.Translations = _hell;
            This.Translations = _this;
            sublime.Translations = _sublime1;

             //now save the dictionary item and translations to Umbraco    
            UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
            var remove = new Sql("DELETE FROM cmsLanguageText");
            int rem = db.Execute(remove);
            ls.Save(hello);
            ls.Save(submit);
            ls.Save(form);
            ls.Save(btstrp);
            ls.Save(world);
            ls.Save(heaven);
            ls.Save(hell);
            ls.Save(This);
            ls.Save(sublime);


    }
[System.Web.Http.AcceptVerbs(“GET”、“POST”)]
公共void SaveLT()
{
var ls=ApplicationContext.Current.Services.LocalizationService;
//为项目的字典翻译创建一个保持架
列表_hello=新列表();
列表_submit=新列表();
列表_form=新列表();
List _bootstrap=newlist();
列表_world=新列表();
列表=新列表();
列表_hell=新列表();
列表_this=新列表();
列表_subgrame1=新列表();
//DictionaryItem的构造函数需要翻译文本的Umbraco语言对象和值
//所以获取语言对象,例如从Iso代码中获取
var language=ls.GetLanguageByIsoCode(“he IL”);
var lang1=ls.GetLanguageByIsoCode(“ru”);
var lang2=ls.GetLanguageByIsoCode(“en-US”);
//在这里,我们为我们的项目创建一个法语翻译,并将其添加到列表中
DictionaryTranslation hebhello=新字典翻译(语言,“שלום”);
DictionaryTranslation-rushello=新字典翻译(lang1,“аааааааааааа1072;
DictionaryTranslation enghello=新字典翻译(lang2,“Blah”);
_你好,Add(hebhello);
_你好,Add(rushello);
_hello.Add(enghello);
DictionaryTranslation hebsubmit=新字典翻译(语言,“שלח”);
DictionaryTranslation russubmit=新的DictionaryTranslation(lang1,“аПааааааааа;
DictionaryTranslation engsubmit=新字典翻译(lang2,“提交”);
_提交。添加(hebsubmit);
_提交。添加(提交);
_提交。添加(engsubmit);
DictionaryTranslation hebform=新字典翻译(语言,“טופס”);
DictionaryTranslation rusform=新的DictionaryTranslation(lang1,“фааааааааа1072;
DictionaryTranslation engform=新字典翻译(lang2,“形式”);
_表格。添加(HEB表格);
_表格.增补(俄罗斯表格);
_表格.增补(英文表格);
DictionaryTranslation hebbtstrp=新字典翻译(语言“אֹזֶַַַַל”);
DictionaryTranslation rusbtstrp=新字典翻译(lang1,“аггззззззззззззз;
DictionaryTranslation engbtstrp=新的DictionaryTranslation(lang2,“Bootstrap”);
_bootstrap.Add(hebbtstrp);
_bootstrap.Add(rusbtstrp);
_bootstrap.Add(engbtstrp);
DictionaryTranslation hebworld=新字典翻译(语言“עוָֹֹם”);
DictionaryTranslation rusworld=新字典翻译(lang1,“Мцц”);
字典翻译英语世界=新字典翻译(lang2,“世界”);
_添加(hebworld);
_添加(俄罗斯世界);
_世界。添加(英语世界);
DictionaryTranslation hebheaven=新字典翻译(语言,“•ןהעדן”);
DictionaryTranslation rusheaven=新字典翻译(lang1,“ббббб”);
字典翻译英语天堂=新字典翻译(lang2,“天堂”);
_天堂。加上(赫布哈文);
_天堂。加上(拉什伊文);
_天堂。添加(英语天堂);
DictionaryTranslation hebhell=新字典翻译(语言“•ֵֵיִנוֹם”);
DictionaryTranslation rushell=新字典翻译(lang1,“ааа”);
字典翻译英语地狱=新字典翻译(lang2,“地狱”);
_地狱加(赫贝尔);
_加上(拉塞尔);
_添加(enghell);
字典翻译hebthis=新字典翻译(语言,“זֶה”);
字典翻译rusthis=新字典翻译;
DictionaryTranslation engthis=新的DictionaryTranslation(lang2,“This”);
_这个。添加(hebthis);
_这个。加上(锈迹);
_th