On this page
You can update Snowflake data either by modifying the data returned by the SnowflakeDataAdapter class and then calling its Update method, or by executing the corresponding DML statements (INSERT, DELETE, UPDATE, MERGE) via SnowflakeCommand.
The samples below use the dept table from the schema specified in the Schema connection string parameter. You can create it and fill it with data with the following statements:
|
|---|
CREATE TABLE dept (deptno INT, dname VARCHAR(14), loc VARCHAR(13));
INSERT INTO dept VALUES (10, 'ACCOUNTING', 'NEW YORK'), (20, 'RESEARCH', 'DALLAS'), (30, 'SALES', 'CHICAGO'), (40, 'OPERATIONS', 'BOSTON'); |
If you use SnowflakeCommandBuilder to generate the INSERT, UPDATE, and DELETE commands for a data adapter, note the following:
- The command builder identifies rows by the primary key declared for the table. Snowflake doesn't enforce primary keys, but the declaration is still needed here. If the table has no primary key, the command builder identifies rows by one of the columns of the query, which may be not unique, so an update or a deletion can change several rows; if it can't choose such a column, it throws InvalidOperationException. For such tables, declare a primary key or set the UpdateCommand and DeleteCommand properties of the data adapter yourself.
- The WHERE clause of the generated UPDATE and DELETE statements contains only the key columns. If a row was changed in Snowflake after you filled the DataTable, your changes overwrite these changes. If the row was deleted, DBConcurrencyException is thrown.
- Names that are not plain uppercase identifiers and names that are reserved words are quoted automatically, even if the Quoted property of the command builder is false.
- Batch updates are not supported. Keep the UpdateBatchSize property of the data adapter set to 1: with a greater value, the Update method fails with the "Batch updates are not supported yet" error.
Here is an example showing how to update Snowflake data using SnowflakeDataAdapter.
|
|---|
using System;
using System.Data;
using Devart.Data.Snowflake;
...
class Program
{
static void Main(string[] args) {
const string connectionString = "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********";
const string sql = "SELECT deptno, dname, loc FROM dept ORDER BY deptno";
using (SnowflakeConnection connection = new SnowflakeConnection(connectionString)) {
connection.Open();
DataTable table = new DataTable("Dept");
using (SnowflakeCommand command = connection.CreateCommand()) {
command.CommandText = sql;
using (SnowflakeDataAdapter adapter = new SnowflakeDataAdapter(command)) {
adapter.Fill(table);
adapter.UpdateCommand = new SnowflakeCommand("UPDATE dept SET loc = :loc WHERE deptno = :deptno", connection);
adapter.UpdateCommand.Parameters.Add("deptno", DbType.Int64).SourceColumn = "deptno";
adapter.UpdateCommand.Parameters["deptno"].SourceVersion = DataRowVersion.Original;
adapter.UpdateCommand.Parameters.Add("loc", DbType.String).SourceColumn = "loc";
DataRow firstrow = table.Rows[0];
firstrow["loc"] = "BOSTON";
Console.WriteLine(adapter.Update(table));
}
}
Console.WriteLine("Rows after update.");
foreach (DataRow row in table.Rows) {
Console.WriteLine("{0}\t{1}\t{2}", row[0], row[1], row[2]);
}
}
Console.ReadKey();
}
}
|
|
|---|
Imports System
Imports System.Data
Imports Devart.Data.Snowflake
...
Module Program
Sub Main()
Const connectionString As String = "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********"
Const sql As String = "SELECT deptno, dname, loc FROM dept ORDER BY deptno"
Using connection As New SnowflakeConnection(connectionString)
connection.Open()
Dim table As New DataTable("Dept")
Using command As SnowflakeCommand = connection.CreateCommand()
command.CommandText = sql
Using adapter As New SnowflakeDataAdapter(command)
adapter.Fill(table)
adapter.UpdateCommand = New SnowflakeCommand("UPDATE dept SET loc = :loc WHERE deptno = :deptno", connection)
adapter.UpdateCommand.Parameters.Add("deptno", DbType.Int64).SourceColumn = "deptno"
adapter.UpdateCommand.Parameters("deptno").SourceVersion = DataRowVersion.Original
adapter.UpdateCommand.Parameters.Add("loc", DbType.String).SourceColumn = "loc"
Dim firstrow As DataRow = table.Rows(0)
firstrow("loc") = "BOSTON"
Console.WriteLine(adapter.Update(table))
End Using
End Using
Console.WriteLine("Rows after update.")
For Each row As DataRow In table.Rows
Console.WriteLine("{0}" & vbTab & "{1}" & vbTab & "{2}", row(0), row(1), row(2))
Next
End Using
Console.ReadKey()
End Sub
End Module
|
The following example updates Snowflake data using SnowflakeCommand.
|
|---|
using System;
using Devart.Data.Snowflake;
...
class Program
{
static void Main(string[] args) {
const string connectionString = "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********";
const string sql = "UPDATE dept SET loc = 'SEATTLE' WHERE dname = 'SALES'";
using (SnowflakeConnection connection = new SnowflakeConnection(connectionString)) {
connection.Open();
using (SnowflakeCommand command = connection.CreateCommand()) {
command.CommandText = sql;
Console.WriteLine(command.ExecuteNonQuery());
}
}
Console.ReadKey();
}
}
|
|
|---|
Imports System
Imports Devart.Data.Snowflake
...
Module Program
Sub Main()
Const connectionString As String = "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********"
Const sql As String = "UPDATE dept SET loc = 'SEATTLE' WHERE dname = 'SALES'"
Using connection As New SnowflakeConnection(connectionString)
connection.Open()
Using command As SnowflakeCommand = connection.CreateCommand()
command.CommandText = sql
Console.WriteLine(command.ExecuteNonQuery())
End Using
End Using
Console.ReadKey()
End Sub
End Module
|
See Also
Entity Framework Core Support
| Retrieving Data
| Using Parameters