一切似乎都没问题,我想从SQL中读取行时会看到,但在添加行时它们不会出现在SQL Server中(是的,我确实刷新了数据库)。 这是我的连接字符串:
connetionString = "Driver={Sql Server}; Server=baxu\\sqlexpress; Database = baza1;" + $"UID ={ username };PWD={ password };";
这里是添加行的代码:
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;
}
答案 0 :(得分:0)
@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();
}
}