user11131551
user11131551

Reputation:

How can I create a model to store data from Database with accented letters?

Normally when a stored procedure is called, we have to set up a model for it. So a basic model would be like this.

Database:

TABLE [dbo].[ExampleWithAccentedLetters](
    [Ngay] [DateTime] ,
    [Ma_Cttv] [int]

Database item column without accented letters

Then my object model is easy and simple to set up with just:

public class DataModel
{
    public DateTime Ngay { get; set; }
    public int Ma_Cttv { get; set; }
}

But what if a stored procedure returns me a table with a column with accented letters or special characters like this.

TABLE [dbo].[ExampleWithAccentedLetters](
    [Ngay] [DateTime] ,
    [Ma_Cttv] [int]
    [Điện tự phát] [int]

Database with accented letters column title

How can I implement my model now?

Upvotes: 0

Views: 179

Answers (2)

tmaj
tmaj

Reputation: 35037

I think ColumnAttribute could work here:

public class DataModel
{
    public DateTime Ngay { get; set; }

    public int Ma_Cttv { get; set; }

    [Column(Name = "Điện tự phát")]
    int Column3 {get; set;}
}

Upvotes: 1

Luke Weaver
Luke Weaver

Reputation: 409

I'm assuming you can't just rename the column which would probably easiest solution. You might have to return a view and rename the column in the view. You can create model classes for views like you can for tables.

EDIT

You can create a view and rename it's columns so your c# class can use the new non-accented column name(s) like so:

create view nonAccentedView (Ngay, Ma_Cttv, DienTuPhat)
as
select [Ngay], [Ma_Cttv], [Điện tự phát]
from [dbo].[ExampleWithAccentedLetters]

Now your c# class can look like this when you return the view:

public class NonAccentedView {
    public DateTime Ngay { get; set; }
    public int Ma_Cttv { get; set; }
    public int DienTuPhat { get; set; }
}

Upvotes: 0

Related Questions