public void SetInt32(int ordinal, int value, bool isPrimary = false) { if (_found && !isPrimary) { _resultSet.SetInt32(ordinal, value); } else if (!_found) { _newRecord.SetInt32(ordinal, value); } }
private void FillInProperty(PropertyInfo property, int ordinal, SqlCeUpdatableRecord record, object value) { if (value.GetType() == typeof(bool)) { record.SetBoolean(ordinal, (bool)value); } else if (value.GetType() == typeof(Int16)) { record.SetInt16(ordinal, (Int16)value); } else if (value.GetType() == typeof(Int32)) { record.SetInt32(ordinal, (Int32)value); } else if (value.GetType() == typeof(Int16)) { record.SetInt64(ordinal, (Int16)value); } }
private static void SetData(SqlCeUpdatableRecord rec, int index, Type type, TypeConvertableWrapper col) { switch (Type.GetTypeCode(type.GetType())) { case TypeCode.String: rec.SetString(index, col.String); break; case TypeCode.Int16: rec.SetInt16(index, col.Int16); break; case TypeCode.Int32: rec.SetInt32(index, col.Int); break; case TypeCode.Int64: rec.SetInt64(index, col.Int); break; case TypeCode.Byte: rec.SetSqlByte(index, col.Byte); break; case TypeCode.DateTime: rec.SetSqlDateTime(index, col.DateTime); break; case TypeCode.Decimal: rec.SetDecimal(index, col.Decimal); break; default: throw new ArgumentException(Type.GetTypeCode(type.GetType()).ToString() + "型には対応していません。"); } }
private void button1_Click(object sender, RoutedEventArgs e) { int recordCount = 10000; using (PopupForm p = new PopupForm()) { p.X_NotifyStr = "Record Count"; if (p.ShowDialog() == System.Windows.Forms.DialogResult.OK) { int outValue; if (!int.TryParse(p.X_Result, out outValue)) { return; } recordCount = outValue; } else { return; } } string testDb = DateTime.Now.Second.ToString() + "test.sdf"; string testTable = "testTable"; if (!App.MainEngineer.CreateDatabase(new LoginInfo_SSCE() { DbName = testDb })) { return; } App.MainEngineer.Open(new CoreEA.LoginInfo.LoginInfo_SSCE() { DbName = testDb, Pwd = "", IsEncrypted = false }); if (!App.MainEngineer.IsOpened) { throw new Exception("Can't Open"); } //List<CreateTableArgs> argsList=new List<CreateTableArgs>(); //CreateTableArgs args=new CreateTableArgs(); //args.allowNulls = false; //args.dataLength = 0; //args.dataType="int"; //args.fieldName="id"; //args.isUnique = false; //args.isPrimaryKey = false; //argsList.Add(args); BaseTableSchema tableSchame = new BaseTableSchema(); tableSchame.Columns.Add(new BaseColumnSchema() { ColumnName = "id", ColumnType = "int" }); try { App.MainEngineer.CreateTable(tableSchame); string sqlCmd = string.Empty; SqlCeConnection conn = new SqlCeConnection(string.Format("Data source={0}", testDb)); SqlCeCommand cmd = new SqlCeCommand(); cmd.Connection = conn; conn.Open(); Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < recordCount; i++) { sqlCmd = string.Format("insert into {0} values ({1})", testTable, i); cmd.CommandText = sqlCmd; cmd.ExecuteNonQuery(); } watch.Stop(); long sqlDirectTime = watch.ElapsedMilliseconds; watch.Reset(); watch.Start(); cmd.CommandText = string.Format("INSERT INTO {0} VALUES (?)", testTable); cmd.Parameters.Add("@id", SqlDbType.Int); cmd.Prepare(); for (int i = 0; i < recordCount; i++) { cmd.Parameters[0].Value = i; cmd.ExecuteNonQuery(); } watch.Stop(); long sqlParaTime = watch.ElapsedMilliseconds; watch.Reset(); watch.Start(); cmd.CommandText = testTable; cmd.CommandType = CommandType.TableDirect; SqlCeResultSet st = cmd.ExecuteResultSet(ResultSetOptions.Updatable); SqlCeUpdatableRecord rec = st.CreateRecord(); for (int i = 0; i < recordCount; i++) { rec.SetInt32(0, i); st.Insert(rec); } watch.Stop(); long sqlceResultSetTime = watch.ElapsedMilliseconds; //watch.Start(); //cmd.CommandText = testTable; //cmd.CommandType = CommandType.TableDirect; //SqlCeResultSet st = cmd.ExecuteResultSet(ResultSetOptions.Updatable); //SqlCeUpdatableRecord rec = //for (int i = 0; i < recordCount; i++) //{ // rec.SetInt32(0, i); // st.Insert(rec); //} //watch.Stop(); long sqlceUpdateResultSetTime = 100;// watch.ElapsedMilliseconds; cmd.Dispose(); conn.Close(); MessageBox.Show(string.Format("Test Result is \r\nDirect sql command used {0} \r\nUse parameters used{1}\r\nUse SqlceResultSet used{2}\r\nUpdate Sqlce ResultSet{3}\r\n", sqlDirectTime, sqlParaTime, sqlceResultSetTime, sqlceUpdateResultSetTime)); } catch (Exception ee) { ProcessException.DisplayErrors(ee); } }
/// <summary> /// Returns generated Id. /// </summary> /// <returns></returns> public static Int32 Write_Level1(Level1 level1, SqlCeConnection conn) { Dictionary <string, Int32> idBag = new Dictionary <string, int>(); // Level1 using (SqlCeCommand cmd = new SqlCeCommand(level1.GetType().Name, conn)) { cmd.CommandType = System.Data.CommandType.TableDirect; using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable)) { SqlCeUpdatableRecord r = rs.CreateRecord(); r.SetString(1, level1.Value); // Payload rs.Insert(r, DbInsertOptions.PositionOnInsertedRow); idBag[level1.GetType().Name] = rs.GetInt32(0); } } foreach (var level2 in level1.levels) // Payload { // Level2 using (SqlCeCommand cmd = new SqlCeCommand(level2.GetType().Name, conn)) { cmd.CommandType = System.Data.CommandType.TableDirect; using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable)) { SqlCeUpdatableRecord r = rs.CreateRecord(); r.SetInt32(GetOrdinal(r, level2.GetType(), level1.GetType()), idBag[level1.GetType().Name]); // foreign key r.SetString(1, level2.Value); // payload rs.Insert(r, DbInsertOptions.PositionOnInsertedRow); idBag[level2.GetType().Name] = rs.GetInt32(0); } } foreach (var level3 in level2.levels) { using (SqlCeCommand cmd = new SqlCeCommand(level3.GetType().Name, conn)) { cmd.CommandType = System.Data.CommandType.TableDirect; using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable)) { SqlCeUpdatableRecord r = rs.CreateRecord(); r.SetInt32(GetOrdinal(r, level3.GetType(), level2.GetType()), idBag[level2.GetType().Name]); r.SetString(1, level3.Value); rs.Insert(r, DbInsertOptions.PositionOnInsertedRow); idBag[level3.GetType().Name] = rs.GetInt32(0); } } foreach (var level4 in level3.levels) { using (SqlCeCommand cmd = new SqlCeCommand(level4.GetType().Name, conn)) { cmd.CommandType = System.Data.CommandType.TableDirect; using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable)) { SqlCeUpdatableRecord r = rs.CreateRecord(); r.SetInt32(GetOrdinal(r, level4.GetType(), level3.GetType()), idBag[level3.GetType().Name]); r.SetString(1, level4.Value); rs.Insert(r, DbInsertOptions.PositionOnInsertedRow); idBag[level4.GetType().Name] = rs.GetInt32(0); } } foreach (var level5 in level4.levels) { using (SqlCeCommand cmd = new SqlCeCommand(level5.GetType().Name, conn)) { cmd.CommandType = System.Data.CommandType.TableDirect; using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable)) { SqlCeUpdatableRecord r = rs.CreateRecord(); r.SetInt32(GetOrdinal(r, typeof(Level5), typeof(Level4)), idBag[level4.GetType().Name]); r.SetString(1, level5.Value); rs.Insert(r, DbInsertOptions.PositionOnInsertedRow); } } } } } } return(idBag[level1.GetType().Name]); }