简体   繁体   English

行未添加到SQL(ODBC连接)

[英]Rows are not being added to SQL (ODBC connection)

Everything seems to be ok, I see rows when I want to read from SQL, but when I'm adding them they do not appear in SQL Server (Yes I did refresh the database). 一切似乎都很好,我想从SQL中读取行时会看到行,但是在添加行时它们不会出现在SQL Server中(是的,我确实刷新了数据库)。 Here is my connection string: 这是我的连接字符串:

connetionString = "Driver={Sql Server}; Server=baxu\\sqlexpress; Database = baza1;" + $"UID ={ username };PWD={ password };";

And here the code where I add rows: 这是我添加行的代码:

cmdString = $"INSERT INTO {n} VALUES (?, ?);";
using (OdbcCommand comm = new OdbcCommand())
{
    comm.Connection = connection;
    comm.CommandText = cmdString;
    comm.Parameters.Add("value", System.Data.Odbc.OdbcType.Int).Value = value;
    comm.Parameters.Add("time", System.Data.Odbc.OdbcType.DateTime).Value = System.DateTime.Now;
    comm.ExecuteNonQuery();
    rows += 1;
}

@Ko Su, try this: I noticed that your definition of odbc connection variable and create command is the same. @Ko Su,请尝试以下操作:我注意到您对odbc连接变量和create命令的定义是相同的。

using System;
using System.Data;
using System.Data.Odbc;

   class OdbcUpdate
   {
      static void Main()
      {

      OdbcConnection comm = new OdbcConnection("Driver={Sql Server}; Server=baxu\\sqlexpress; Database = baza1;" + $"UID ={ username };PWD={ password };"); 

      comm.Open();

      OdbcCommand myOdbcCommand = comm.CreateCommand();

      myOdbcCommand.CommandText = "INSERT INTO table1 (field1, field2) VALUES (?, ?)";

      myOdbcCommand.Parameters.Add("@field1", OdbcType.Int);
      myOdbcCommand.Parameters.Add("@field2", OdbcType.Int);

      myOdbcCommand.Parameters["@field1"].Value = 1;
      myOdbcCommand.Parameters["@field2"].Value = 2;

      Console.WriteLine("Number of Rows Affected is: {0}", myOdbcCommand.ExecuteNonQuery());

      comm.Close();

      Console.ReadKey();

      }
   }

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

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