user153923
user153923

Reputation:

C#: SqlCeParameter DbType for nVarChar?

First off, I like specifying data types and I despise AddWithValue functions.

I'm building an SqlCeServer 3.5 local database Application under .NET 4.0 that will run on users PC.

When I created the table, I use NVarChar(50) to specify my string fields.

This seems to work fine, and I can open the table to verify everything worked well in Microsoft SQL Server Management Studio 2008.

When I insert data into my tables, I use

public static SqlCeParameter ParameterString(string ColumnName, string value) {
  SqlCeParameter p = new SqlCeParameter();
  p.ParameterName = string.Format("@{0}", ColumnName);
  p.DataType = DbType.String;
  p.Size = 50;
  p.Value = value;
  return p;
}

When I insert this data, there are no errors, but the data is not inserted. Other data types (int, DateTime, float, etc.) are inserting with no problems.

Q: Is there some other DbType I need to specify to insert as NVarChar?

Upvotes: 3

Views: 6669

Answers (3)

Chris Haas
Chris Haas

Reputation: 55427

For some reason there's two properties that you can use to set the type, DbType and SqlDbType. According to MSDN:

SqlDbType and DbType are linked. Therefore, setting the DbType changes the SqlDbType to a supporting SqlDbType.

So SqlDbType is the primary type, setting just DbType changes it to something close.

You might also want to read up on this post where they had a problem when specifying Lengths greater than 255.

Upvotes: 2

Guffa
Guffa

Reputation: 700362

The DbType enum is generic for all databases, so it doesn't contain all data types specific for SQL Server. Use an SqlDbType:

SqlCeParameter p = new SqlCeParameter(ColumnName, SqlDbType.NVarChar, 50);
p.Value = value;

Upvotes: 4

Senad Meškin
Senad Meškin

Reputation: 13756

yes,

SqlDbType.NVarChar

I hope this helps

Upvotes: 1

Related Questions