Skip to content

Instantly share code, notes, and snippets.

@negonicrac
Created October 11, 2012 15:56
Show Gist options
  • Save negonicrac/3873377 to your computer and use it in GitHub Desktop.
Save negonicrac/3873377 to your computer and use it in GitHub Desktop.
Can you convert NVarChar into a string?
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