我尝试TDD使用SubSonic 2.1,MySQL和C#,当我使用下面的代码测试更新现有记录时,
public void Test2()
{
User.Insert("jmarcus1", "jmarcus1", "jackass", "marcus", 3, false);
User users = new User();
int ulevel = 1;
User.Update(2, "jmarcus1", "jmarcus1", "jackass", "marcus", ulevel, false);
Assert.AreEqual(1, users.Ulevel);
}
以下
------ Test started: Assembly: SalMan.dll ------
Starting the MbUnit Test Execution
Exploring SalMan, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
MbUnit 2.4.2.130 Addin
Found 3 tests
[success] TestFixture1.Test
[success] TestFixture1.Test1
[failure] TestFixture1.Test2
TestCase 'TestFixture1.Test2'
failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `userid` = 1; SELECT 1 AS id' at line 1
MySql.Data.MySqlClient.MySqlException
Message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `userid` = 1; SELECT 1 AS id' at line 1
Source: MySql.Data
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.OpenPacket()
at MySql.Data.MySqlClient.NativeDriver.ReadResult(UInt64& affectedRows, Int64& lastInsertId)
at MySql.Data.MySqlClient.MySqlDataReader.GetResultSet()
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar()
C:\svn\subsonicproject\trunk\SubSonic\DataProviders\MySqlDataProvider.cs(280,0): at SubSonic.MySqlDataProvider.ExecuteScalar(QueryCommand qry)
C:\svn\subsonicproject\trunk\SubSonic\DataProviders\DataService.cs(533,0): at SubSonic.DataService.ExecuteScalar(QueryCommand cmd)
C:\svn\subsonicproject\trunk\SubSonic\ActiveRecord\ActiveRecord.cs(182,0): at SubSonic.ActiveRecord`1.Save(String userName)
D:\My Documents\Visual Studio 2008\Projects\SalMan\SalMan\Generated\User.cs(352,0): at Salman.User.Update(Int32 varUserid, String varUsername, String varPassword, String varFname, String varLname, Int32 varUlevel, Boolean varStatus)
D:\My Documents\Visual Studio 2008\Projects\SalMan\SalMan\Tests\TestFixture1.cs(40,0): at salman.TestFixture1.Test2()
[reports] generating HTML report
TestResults: file:///C:/Documents%20and%20Settings/*****************/Application%20Data/MbUnit/Reports/SalMan.Tests.html
2 passed, 1 failed, 0 skipped, took 7.66 seconds.
有没有人有解决方法呢?
答案 0 :(得分:2)
在我看来,你的args已经失灵了。 PK应该是第一件事 - 你确定他们排队正确吗?
您使用的是ActiveRecord吗?如果是这样 - 你试过这个:
User u=new User();
u.UserName("jmarcus1");
u.Name="jackass"
u.Ulevel=1;
...
u.Save();
User u=new User("jmarcus1");
u.UILevel=2;
u.Save();
答案 1 :(得分:1)
显然,根据Robs上面的优秀建议,解决方案是
User u = User.FetchByID(2);
u.Ulevel = ulevel;
u.save();
我不太确定上面的代码段会在您指定之前用于更新 您想要更新的记录。
我对Rob的问题是,为什么“u.Update(2,”jmarcus“,......”给出错误 以上?