Created
October 11, 2012 15:56
-
-
Save negonicrac/3873377 to your computer and use it in GitHub Desktop.
Can you convert NVarChar into a string?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var r = new DataSet(); | |
try | |
{ | |
var connectionString = "database-connection-string"; | |
using (DbConnection connection = new SqlConnection(connectionString: connectionString)) | |
{ | |
connection.Open(); | |
using (DbCommand command = connection.CreateCommand()) | |
{ | |
command.CommandText = "Select [F].[Foo] FROM [dbo].[Foos] [F] WHERE [F].[ID] = @ID"; // [F].[Foo] is a NVarChar column in this example | |
command.Parameters.Add(new SqlParameter { ParameterName = "ID", DbType = DbType.Int32, Value = 1 }); // Always use parameters to protect yourself from SQL injecteion | |
var dataAdapter = new DbDataAdapter(); | |
dataAdapter.SelectCommand = command; | |
dataAdapter.Fill(r); | |
} | |
} | |
} | |
catch (Exception exception) | |
{ | |
throw exception; | |
} | |
string value = r.Tables[0].Rows[0]["Foo"] // will return a string value. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment