최근까지 실제로 잘 작동하는 것처럼 보인 데이터 이전 도구가 있습니다.멀티 스레딩 할 때 식별 열에 캔트 삽입
Parallel.Foreach를 사용하여 데이터 행 모음을 실행하고 테이블의 새 레코드에 삽입 할 변수를 계산 한 다음 SQL 문을 실행하여 데이터를 삽입하십시오. I 집계 예외가 그러나 다음
Parallel.ForEach<DataRow>(dataTable.Rows, row =>
{
string reference = row["ref"].ToString();
int version = (int)row["version"];
string insertStatement = "INSERT INTO Item (Reference, Version) VALUES (@reference, @version)";
_database.ExecuteCommand(insertStatement, new SqlServerCeParameter[]
{
new SqlServerCeParameter("@reference", reference, SqlDbType.NVarChar),
new SqlServerCeParameter("@version", version, SqlDbType.Int),
});
});
public void ExecuteCommand(string sql, SqlServerCeParameter[] parameters)
{
//create the command that will execute the Sql
using (var command = new SqlCeCommand(sql, _connection))
{
//add any parameters
if (parameters != null) command.Parameters.AddRange(parameters.Select(p => p.ParameterBehind).ToArray());
try
{
//open the connection
if (_connection.State == ConnectionState.Closed) _connection.Open();
//execute the command
command.ExecuteNonQuery();
}
catch (SqlCeException ex)
{
//only catch if the native error is the duplicate value exception otherwise throw as normal
if (ex.NativeError != 25016) throw;
}
finally
{
//explicitly close command
command.Dispose();
}
}
}
는의 내부 예외이다
{"The column cannot contain null values. [ Column name = ID,Table name = Item ]"}
다음과 같이 테이블의 구조는 다음과
CREATE TABLE Item
(
ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Reference NVARCHAR(50) NOT NULL,
Version INT NOT NULL
);
이제 돈 ' ID가 ID 열이므로이 오류를 이해해야합니다.
멀티 스레딩 때문에 동시에 두 ID를 계산할 수 없다는 생각이 들었지 만, 이것은 다중 사용자 환경에서 SqlServerCe가 정상적인 의미를 갖는 것처럼 보이지 않습니다.
멀티 스레딩 세계에 오신 것을 환영합니다! 두 스레드가 둘 다 병렬로 실행 중이기 때문에 두 개의 스레드가 ID : 2 레코드를 삽입하기로 결정할 수 있습니다 ... 하나가 손실됩니다 ... – Belogix
@Belogix가 SQL Server Compact에서 자동으로 처리하지 못합니까 ??? – DavidG
ID 열을 문제가되는 기사로 제공하는 예외 메시지로만 이동합니다. –