Create database with UTF8 encoding - c#

I'd want to create database with entityframework / npgsql driver with code-first approach but also UTF encoding.
In PGAdmin 3 I'm unable to change Encoding because its field is readonly
This sql is being shown in PGAdmin when I select my database
CREATE DATABASE db
WITH OWNER = dbuser
ENCODING = 'LATIN2' <------------
TABLESPACE = pg_default
LC_COLLATE = 'pl_PL'
LC_CTYPE = 'pl_PL'
CONNECTION LIMIT = -1;
But I want to change it from LATIN2 to UTF8
I manually tried this:
SET CLIENT_ENCODING TO 'UTF8'
and
show client_encoding
returns
client_encoding_text
UTF8
but despite that PGAdmin 3 still shows that this database has an encoding "LATIN2"
and my application is receiving an exception
PostgresException: 22P05: the character 0xe2889e coding "UTF8" does not have an equivalent in "LATIN2"
So, how can I create db with UTF8 via Entityframework with NPSQL driver for postgre using context.Database.EnsureCreated()?

Related

Load data infile parameter error

LOAD data LOCAL INFILE
'C:/ProgramData/MySQL/MySQL Server 5.6/Uploads/amazon1.xml'
INTO TABLE
amazonxmlfeeddata
CHARACTER SET 'utf8'
LINES STARTING BY '<item_data>' TERMINATED BY '</item_data>'
(#tmp)
SET
item_unique_id = ExtractValue(#tmp, '//item_unique_id'),
item_title = ExtractValue(#tmp, '//item_title'),
item_long_desc = ExtractValue(#tmp, '//item_long_desc'),
item_page_url = ExtractValue(#tmp, '//item_page_url');
This is basically my query and I wrote this is in ASP.NET C#.
I want to execute this through C# in MySQL.
But if I execute through C# it gives me error of (#tmp) that it should be declared. And even after declaring it still throws the same error.
It seems you need to add AllowUserVariables=true; to the MySql connection string, to allow user variables (#tmp).
See: https://dev.mysql.com/doc/connector-net/en/connector-net-connection-options.html

MySql command "load data infile" execution error in C#

I am trying to import a text file into MySql database using C# code but getting errors.
My table structure is:
and the C# code that I'm executing is:
fileQuery =
"load data infile '{0}' into table dgl.deliveries fields terminated by '\t' lines terminated by \r\n' (#ImagePath, Delivery_Note, Shipment_Number, #Delivery_Date, Deliver_To_Code, Deliver_To_Name, Sold_To_Code, Sold_To_Name, Material_Number, Doctype) set Delivery_Date = tr_to_date(#Delivery_Date, '%d/%m/%Y'), ImagePath = Concat('USERFILES/', #ImagePath)";
string q = string.Format(fileQuery,fileName);
MySqlConnection conn = new MySqlConnection(dglConnection.ConnectionString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = q;
conn.Open();
command.ExecuteNonQuery();
and the error is:
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.DLL but was not handled in user code
Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%d/%m/%Y'), ImagePath = Concat('USERFILES/', #ImagePath)' at line 2
The following is a line from source input file:
123.pdf 802661341 1061611 18/02/2015 00:00:00 22280 ABC LIMITED 22280 XYZ LIMITED 30679795 30744488 DELIVERY NOTE 1
Your problem is that Your Date that you are passing from your C# code is 18/02/2015. Mysql only excepts a date in the format YYYY-MM-DD. You need to adjust that data so that it formats to the way Mysql will except a date if you want to store it as a date.
I actually wrote a stored procedure that you maybe able to use (or at least get an idea of what needs to be done): Here is the link.
Also when in doubt just refer to dev.mysql its a great resource also.

Entity Framework Seems to want to use Sql Server Provider despite needing Sql CE

I am using an SqlCe 4 database with an Entity Framework 6 code first data model.
I set the connection string in the DbContext constructor like so:
private static readonly string DATABASE_PASSWORD = "...";
private static readonly string CONNECTION_STRING = string.Format(#"Data Source={0};Password={1}", MyProject.Properties.Settings.Default.DatabaseLocation, DATABASE_PASSWORD);
public DataModel()
: base(CONNECTION_STRING)
{
}
And when I run my application which uses this model it works.
In the designer in Visual Studio however it keeps freezing for about 30 seconds at a time when I try to view a WPF control that load a set of records in the View model constructor and I get an error message as follows:
Error 2 Unable to complete operation. The supplied SqlConnection does not specify an initial catalog or AttachDBFileName.
I am not using SqlServer I am using Sql Server Compact so there should be no need to even think about using Sql Server.
Can anyone see anything wrong with my connection string?
Viewing the error message ( ...The supplied SqlConnection.. ) I conclude that you are setting an SQlConection rather than SQlCeConnection
Make sure you set your SQL object like:
conn = new SqlCeConnection(CONNECTION_STRING);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "..."
And make sure you have added a reference to the SQL Server Compact provider, System.Data.SqlServerCe.dll

How do I set encoding in an NpgsqlConnection

I have a PostgreSQL database, which uses character encoding WIN1252.
When querying the database, some records will produce an error when trying to read the data, because it is trying to convert it to UTF8. This happens on some foreign names containing certain non-Latin characters.
The error is:
ERROR: 22P05: character with byte sequence 0x81 in encoding "WIN1252" has no equivalent in encoding "UTF8"
It happens when I call Read() on the NpgsqlDataReader.
My connection is defined as:
new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=xyz;User Id=****;Password=****;");
What can I do to read this data using C#?
I've managed to solve the problem. There is no way of setting the property in the connection string or any of the properties of the NpgsqlConnection or NpgsqlCommand.
However, I was able to set the value of client_encoding in a query. So directly after opening the connection I first executed the (non)query:
set client_encoding = 'WIN1252'
After that, any subsequent command on the same connection used the proper encoding and returned the results without complaints.
I tried to change the connection string but i had no luck with that.
The problem got solved by chaning the database settings file and reload it.
So i started pgadmin and executed
SHOW config_file;
which gave me
C:/Program Files/PostgreSQL/14/data/postgresql.conf
in this file i changed the lc_messages from lang_language.1252 to UTF8.
After that i reloaded this config in pg admin by right click on the server name and press "Reload Configuration".
All settings are now set to UTF8 and it just worked fine.
lc_messages = 'UTF8' # locale for system error message
# strings
lc_monetary = 'UTF8' # locale for monetary formatting
lc_numeric = 'UTF8' # locale for number formatting
lc_time = 'UTF8' # locale for time formatting
...

Restore and Backup with Entity Framework

I use C#, .net 4, Entity Framework and SQL Server 2008 R2 in a project.
I have no familiarity with backup and restore from database by Entity Framework. Please help me to write restore and backup code in Entity Framework
Entity Framework is an ORM - object-relational mapper - designed to handle interactions with single entities and/or short lists of entities. It's neither designed for bulk operations, nor is it a server admin framework. So no - I don't think you can do this using Entity Framework - that's not its job.
Use an appropriate tool for the job! Either use SQL Server Management Studio to handle backup/restore - or if you must do it programmatically, use the SMO (Server Management Objects) which is intended for exactly these kinds of jobs
To other friends who have this problem ....
Useing ExecuteSqlCommand can backup of db in EF 6+ .
For example : (this code create backup of your DB , I had tested this.)
string dbname = db.Database.Connection.Database;
string sqlCommand = #"BACKUP DATABASE [{0}] TO DISK = N'{1}' WITH NOFORMAT, NOINIT, NAME = N'MyAir-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand,dbname, "Amin9999999999999"));
backup saved in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Backup
ref=> https://entityframework.codeplex.com/discussions/454994
but I do not recommend for working with this method!
I strongly recommend the use of the article below:
http://www.c-sharpcorner.com/Blogs/8679/backup-and-restore-the-database-in-Asp-Net-web-application.aspx
This should get you going on the restore side:
void LoadDB(
System.Data.Entity.DbContext context,
string backup_filename,
string orig_mdf, // the original LogicalName name of the data (also called the MDF) file within the backup file
string orig_ldf, // the original LogicalName name of the log (also called the LDF) file within the backup file
string new_database_name
)
{
var database_dir = System.IO.Path.GetTempPath();
var temp_mdf = $"{database_dir}{new_database_name}.mdf";
var temp_ldf = $"{database_dir}{new_database_name}.ldf";
var query = #"RESTORE DATABASE #new_database_name FROM DISK = #backup_filename
WITH MOVE #orig_mdf TO #temp_mdf,
MOVE #orig_ldf TO #temp_ldf,
REPLACE;";
context.Database.ExecuteSqlCommand(
// Do not use a transaction for this query so we can load without getting an exception:
// "cannot perform a backup or restore operation within a transaction"
TransactionalBehavior.DoNotEnsureTransaction,
query,
new[] {
new SqlParameter("#backup_filename", backup_filename),
new SqlParameter("#database_dir", database_dir),
new SqlParameter("#new_database_name", new_database_name),
new SqlParameter("#orig_mdf", orig_mdf),
new SqlParameter("#orig_ldf", orig_ldf),
new SqlParameter("#temp_mdf", temp_mdf),
new SqlParameter("#temp_ldf", temp_ldf),
}
);
}
If you don't know them beforehand, the MDF and LDF LogicalName values can be obtained manually or programmatically from a query like this one:
RESTORE FILELISTONLY FROM DISK #backup_filename

Categories