有问题将数据导入数据库表

编程入门 行业动态 更新时间:2024-10-26 14:27:18
本文介绍了有问题将数据导入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好,

我的问题非常简单。我在Umbraco MVC4应用程序中有一个Controller类。我正在为umbraco7开发一个定制的导入/导出插件。

问题不在于代码不起作用,而是SQL查询没有被击中。

虽然foreach循环是:

这是我的代码块:

使用UmbracoImportExportPlugin 。楷模; 使用System; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq; 使用System.Net; 使用System.Web; 使用System.Web.Mvc; 使用Umbraco.Core.Persistence; 使用Umbraco.Web; 使用Umbraco.Web.WebApi; namespace UmbracoImportExportPlugin.App_Code { public class ImportNewDictionaryController:UmbracoAuthorizedApiController { public string basePath; //找到特定路径 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) {抛出新的HttpException("无效文件"); } 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(键中的字符串项) { 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) {抛出新的HttpException("无效文件"); } 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(数据中的字符串lang) { foreach(id中的字符串ident) { 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); 返回id; } } }

请注意这是Umbraco的插件,在你提到我的代码存在SQL风险之前注入,唯一可以访问此功能的人是Back Office用户。当插件准备好部署时,安全性将会更新。

解决方案

forums.asp/1146.aspx/1?MVC

论坛是您应该发布的地方。

Hi there,

My issue is pretty simple. I have a Controller Class within an Umbraco MVC4 application. I'm developing a bespoke import/export plugin for umbraco7.

The issue is not that the code isn't working, it's more that the SQL query isn't being hit.

although the foreach loops are:

Here is my code block:

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; } } }

Please note this is a plugin for Umbraco, before you mention that my code is at risk for SQL injection, the only people with access to this would be Back Office users. Security will be updated when the plugin is ready for deployment.

解决方案

forums.asp/1146.aspx/1?MVC

The forum is where you should post.

更多推荐

有问题将数据导入数据库表

本文发布于:2023-10-22 23:16:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!