我通过这些函数将大约1943条记录插入到SQLCE中。这是我的插入函数。参数来自StringReader(字符串来自webservice)。这个函数执行1943次,大约需要20秒。我删除了表的索引,什么可以我做了改进吗?我只创建了一次mycomm和sqlceresultset。
Public Function Insert_Function(ByVal f_Line() As String, ByRef myComm As SqlCeCommand, ByRef rs As SqlCeResultSet) As String
Try
Dim rec As SqlCeUpdatableRecord = rs.CreateRecord()
rec.SetInt32(0, IIf(f_Line(1) = "", DBNull.Value, f_Line(1)))
rec.SetInt32(1, IIf(f_Line(2) = "", DBNull.Value, f_Line(2)))
rec.SetInt32(2, IIf(f_Line(3) = "", DBNull.Value, f_Line(3)))
rec.SetInt32(3, IIf(f_Line(4) = "", DBNull.Value, f_Line(4)))
rec.SetValue(4, IIf(f_Line5(5) = "", DBNull.Value, f_Line(5)))
rs.Insert(rec)
rec = Nothing
Catch ex As Exception
strerr_col = ex.Message
End Try
Return strerr_col
End Function
答案 0 :(得分:2)
当您尝试设置调用SetInt32的DBNull.Value时,我认为您会遇到异常。我设法使用以下代码在一秒钟内插入一万条记录:
using (var cn = new SqlCeConnection(ConnectionString))
{
using (var cmd = new SqlCeCommand())
{
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "TableName";
cmd.CommandType = CommandType.TableDirect;
using (var rs = cmd.ExecuteResultSet(ResultSetOptions.None))
{
var record = rs.CreateRecord();
using (var reader = new StringReader(input))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var index = 0;
var values = line.Split(new[] { ',' });
record.SetValue(index, values[index++] == string.Empty ? (object)DBNull.Value : values[index - 1]);
record.SetValue(index, values[index++] == string.Empty ? (object)DBNull.Value : values[index - 1]);
record.SetValue(index, values[index++] == string.Empty ? (object)DBNull.Value : values[index - 1]);
record.SetValue(index, values[index++] == string.Empty ? (object)DBNull.Value : values[index - 1]);
record.SetValue(index, values[index++] == string.Empty ? (object)DBNull.Value : values[index - 1]);
rs.Insert(record);
}
}
}
}
}