2012-11-08 33 views
1

我正在运行单元测试,当我试图在数据库中插入数据并获取它后,我什么也没有得到(我试过用DataAdapterDataReader)。插入后立即返回行没有结果

但是,当我插入3秒睡眠(即使1秒钟不工作...)插入和选择之间我得到的结果。

在SQL Server Profiler中,我可以看到执行,插入完成并在选择开始前大约10毫秒完成。

我无法找出这来自

的代码看起来是这样的: 插入方法

SqlCommand command = new SqlCommand(sqlTemplate); 
command.Parameters.Add(Sql4oConstants.Sql4oIdParameterName, SqlDbType.UniqueIdentifier).Value = id; 
command.Parameters.Add(Sql4oConstants.Sql4oTimestampParamterName, SqlDbType.DateTime).Value = DateTime.Now; 

command.CommandTimeout = dataSourceDescription.CommandTimeout; 
DatabaseManager.ExecuteNonQuery(dataSourceDescription.ConnectionString, command); 

Get方法

public static void Fill(string connectionString, DataTable table, SqlCommand command) 
    { 
     try 
     { 
      LogStorageWriter.WriteLogEntry(log, EStorageLevelLog.Debug, string.Format("Execute query: {0}", command.CommandText)); 

      using (SqlConnection conn = new SqlConnection(connectionString)) 
      { 
       command.Connection = conn; 
       using (SqlDataAdapter adapter = new SqlDataAdapter(command)) 
       { 
        adapter.Fill(table); 
       } 
      } 
     } 
     catch (InvalidOperationException e) 
     { 
      LogStorageWriter.WriteLogEntry(log, EStorageLevelLog.Error, string.Format("Exception : {0}", e.ToString())); 
     } 
    } 
+0

向我们显示您的代码。 –

+1

您可以将'SELECT ...'附加到sql中的'INSERT'。然后你可以在一个命令中执行这两个操作 –

+0

请显示一些代码 –

回答

0

我解决它。

事实上,这是因为我的请求使用了CONTAINS。然后我发现使用CONTAINS调用SQL Server索引器来获取数据。但引擎不会立即索引数据。 这就是为什么我必须等待2到3秒才能恢复数据。