Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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
C# &引用;字符串“1”后未闭合的引号;错误_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# &引用;字符串“1”后未闭合的引号;错误

C# &引用;字符串“1”后未闭合的引号;错误,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,“/”应用程序中出现服务器错误 “B”附近的语法不正确。字符后未闭合的引号 字符串',e') 描述:发生未处理的异常 异常详细信息:System.Data.SqlClient.SqlException:不正确 靠近“B”的语法。字符串后未闭合的引号 “,e)” 源错误: 在执行过程中生成了未处理的异常 当前web请求。关于货物来源和位置的信息 可以使用下面的异常堆栈跟踪来识别异常 堆栈跟踪: [SqlException(0x80131904):靠近“B”的语法不正确。未关闭 字符串''e')后面

“/”应用程序中出现服务器错误

“B”附近的语法不正确。字符后未闭合的引号 字符串',e')

描述:发生未处理的异常

异常详细信息:System.Data.SqlClient.SqlException:不正确 靠近“B”的语法。字符串后未闭合的引号 “,e)”

源错误:

在执行过程中生成了未处理的异常 当前web请求。关于货物来源和位置的信息 可以使用下面的异常堆栈跟踪来识别异常

堆栈跟踪:

[SqlException(0x80131904):靠近“B”的语法不正确。未关闭 字符串''e')后面的引号。]
System.Data.SqlClient.SqlConnection.OnError(SqlException异常, 布尔断开连接,操作'1 wrapCloseInAction)+3278868
System.Data.SqlClient.TdsParser.ThroweException和Warning(TdsParserStateObject stateObj,布尔调用方连接锁,布尔异步关闭)+791
System.Data.SqlClient.TdsParser.TryRun(运行行为, SqlCommand cmdHandler、SqlDataReader数据流、, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj、Boolean和dataReady)+4927
System.Data.SqlClient.SqlCommand.RunExecuteOnQueryTds(字符串 methodName,布尔异步,Int32超时,布尔异步写入)+1275
System.Data.SqlClient.SqlCommand.InternalExecuteOnQuery(TaskCompletionSource'1 完成,字符串方法名,布尔sendToPipe,Int32超时, 布尔异步写入)+367
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()+386
HalcytronicsInc.Controllers.ExcellUploadController.Upload(HttpPostedFileBase 上载)在C:\Users\M1037515\Documents\Visual Studio中 2015\Projects\HalcytronicsInc\HalcytronicsInc\Controllers\ExcellUploadController.cs:94 lambda_方法(闭包、控制器库、对象[])+139
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary'2参数)+229
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor ActionDescriptor,IDictionary'2 参数)+35
System.Web.Mvc.c__显示类15.b__12() +80 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext预文本,Func'1 continuation)+453
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext预文本,Func'1 continuation)+453
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,字符串actionName)+533

使用Excel;
使用制度;
使用System.Collections.Generic;
使用系统数据;
使用System.IO;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用HalcytronicsInc.型号;
使用System.Data.SqlClient;
名称空间HalcytronicsInc.控制器
{
公共类ExcellUploadController:控制器
{
公共字符串国家;
公共字符串状态;
公共字符串城市;
公共字符串名称;
公共字符串pno;
//获取:ExcellUpload
公共行动结果索引()
{
返回视图();
}
公共行动结果上载()
{
返回视图();
}
[HttpPost]
//[ValidateAntiForgeryToken]
公共操作结果上载(HttpPostedFileBase上载)
{
if(ModelState.IsValid)
{
if(upload!=null&&upload.ContentLength>0)
{
//ExcelDataReader使用二进制Excel文件,因此需要文件流
//开始。这就是我们避免依赖ACE或Interop的方法:
Stream=upload.InputStream;
//我们返回接口,以便
IExcelDataReader=null;
if(upload.FileName.EndsWith(“.xls”))
{
reader=ExcelReaderFactory.CreateBaryReader(流);
}
else if(upload.FileName.EndsWith(“.xlsx”))
{
reader=ExcelReaderFactory.CreateOpenXmlReader(流);
}
其他的
{
AddModelError(“文件”,“不支持此文件格式”);
返回视图();
}
reader.IsFirstRowAsColumnNames=true;
数据集结果=reader.AsDataSet();
//字符串连接字符串=null;
SqlConnection连接;
SqlCommand命令;
SqlDataAdapter adpter=新的SqlDataAdapter();
connection=new SqlConnection(/*“数据源=A2ML10582;用户ID=sa;密码=***************;集成安全性=true”*/
“数据源=A2ML10582;初始目录=HalcytronicsINCSitecore_主目录;用户ID=sa;密码=***********”
);
//connectionString=“Data Source=172.17.2.13;Initial Catalog=”用户ID=sa密码=**********“集成安全性=true”;
//连接=新的SqlConnection(connectionString);
int i=0;
connection.Open();

对于(i=0;i,正如其他人所说,像这样连接SQL的形式很糟糕。也就是说,导致错误的原因是“city”附近缺少一个引号。即使此代码的每个方面都在您的控制之下,您也应该使用参数化查询

string sql = "insert into SalesRepresentative(" + country + ",'" + state + "'," + city + "','+" + name + "'," + pno + ")";
应该是:

string sql = "insert into SalesRepresentative(" + country + ",'" + state + "','" + city + "','+" + name + "'," + pno + ")";

您应该使用参数化查询来避免由输入错误引起的简单语法错误(正如您在这里忘记添加正确的查询)
string sql = "insert into SalesRepresentative(" + country + ",'" + state + "','" + city + "','+" + name + "'," + pno + ")";
....
DataSet result = reader.AsDataSet();

string cmdText = @"insert into SalesRepresentative
    (@country,@state,@city,@name,@pno)";

// using statement around disposable objects.....
using(SqlConnection connection= new SqlConnection(....))
using(SqlCommand cmd = new SqlCommand(cmdText, connection))
{
    connection.Open();

    // Add all parameters before entering the insert loop        
    cmd.Parameters.Add("@country", SqlDbType.NVarChar);
    cmd.Parameters.Add("@state", SqlDbType.NVarChar);
    cmd.Parameters.Add("@city", SqlDbType.NVarChar);
    cmd.Parameters.Add("@name", SqlDbType.NVarChar);
    cmd.Parameters.Add("@pno", SqlDbType.NVarChar);


    for (i = 0; i < result.Tables[0].Rows.Count; i++)
    {
        country = result.Tables[0].Rows[i].ItemArray[0].ToString();
        state = result.Tables[0].Rows[i].ItemArray[1].ToString();
        city =result.Tables[0].Rows[i].ItemArray[2].ToString();
        name = result.Tables[0].Rows[i].ItemArray[3].ToString();
        pno = result.Tables[0].Rows[i].ItemArray[4].ToString();

        // Set the parameter values 
        cmd.Parameters["@country"].Value = country;
        cmd.Parameters["@state"].Value = state;
        cmd.Parameters["@city"].Value = city ;
        cmd.Parameters["@name"].Value = name;
        cmd.Parameters["@pno"].Value = pno;

        // No need of an SqlDataAdapter here, just execute the command...
        cmd.ExecuteNonQuery();
   }
}
return View(result.Tables[0]);