Appearance
Insert β
Data insertion can be strictly typed using annotated classes or very lax by using standard classes without any special configuration.
To perform an insert, there must be a call to at least two methods:
Into<T>()
orInto("table_name")
to select the target database table.Value<T>(row)
,Value(row)
,Values<T>(row)
orValues(row)
to set the row data to insert.
Single Row β
Insert a single row and retrieve the Auto-Increment ID as long
.
csharp
long insertedId = dbConnector.QueryBuilder().Build(query => query
.Insert()
.Into<ExampleTable>()
.Value(new ExampleTable {
MediumText = "Medium text example value",
Enum = EnumExample.N
})
).Execute<long>();
Multiple Rows β
You can either call Value()
multiple times or call Values()
and provide multiple values at the same time, or call both!
csharp
dbConnector.QueryBuilder().Build(query => query
.Into<ExampleTable>()
.Value(new ExampleTable {
MediumText = "Medium text example value",
Enum = EnumExample.N
})
.Values(new List<ExampleTable> {
new ExampleTable {
Json = @"{""sample_json_field"": ""sample_json_value""}",
Enum = EnumExample.Y,
CustomFieldName = new byte[8] { 81,47,15,21,12,16,23,39 }
},
new ExampleTable {
Longtext = "Long text example value",
Enum = EnumExample.N
}
})
.Insert()
).Execute();
β οΈ Critical: Do not mix rows with and without primary key values in the same batch.
β Dangerous:
csharp
.Values(new List<ExampleTable> {
new ExampleTable { Id = 1, Longtext = "Bad!" }, // β Don't set ID manually!
new ExampleTable { Longtext = "Good!" }
})