繁体   English   中英

为什么这个SQL服务器INSERT查询不执行?

[英]Why does not this SQL server INSERT query execute?

我在一个ASP.NET项目(使用serenity.is )上工作,现在有一个数据库查询的问题:

[HttpGet]
[Route("SimonTest/{id=0}/{obj=0}")]
public ActionResult SimonTest(int id, int obj)
{
    SqlConnection myConn = new SqlConnection(@"Server=(LocalDb)\MSSqlLocalDB;Integrated security=SSPI;database=Serene5_Default_v1");

    // skipped code building the following command :

    command = "INSERT INTO [Serene5_Default_v1].[tcpdump].[Errors] (TimeStp,IdSource,IdDestination,PortSource,PortDestination,ToTheRight,ToTheLeft) VALUES ('11:2','11','1','1','1',1,1);";


    System.Diagnostics.Debug.WriteLine(command);
    SqlCommand myCommand2 = new SqlCommand(listDb, myConn);


    myCommand2.ExecuteNonQuery();

    System.Diagnostics.Debug.WriteLine("Commande exécutée");
    myCommand2.Dispose();


    myConn.Close();
    return View("~/Modules/Default/TcpDump/TcpDumpIndex.cshtml");
}

当我使用Microsoft SQL Server Management Studio执行查询INSERT INTO ...时,没有任何问题,但这里的命令似乎被跳过(没有异常引发并且没有在数据库中写入)

在执行查询之前,您忘记将sql文本分配给命令myCommand2.CommandText = command

可能是因为连接不公开,为什么它没有更新

SqlCommand myCommand2 = new SqlCommand(listDb, myConn.Open());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM