2012-11-08 30 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秒才能恢復數據。