user1192047
user1192047

Reputation: 33

How to execute a stored procedure via odbc using c#?

How to execute a stored procedure via odbc using c#?

I want to execute it without the risk of a sql injection, how can I do it?

Upvotes: 3

Views: 5204

Answers (1)

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

using (OdbcConnection connection = new OdbcConnection(connectionString))
using (OdbcCommand command = connection.CreateCommand())
{
    command.CommandText = commandText //your store procedure name;
    command.CommandType = CommandType.StoredProcedure;

    command.Paremeters.Add("@yourParameter", OdbcType.NChar, 50).Value = yourParameter

    DataTable dataTable = new DataTable();

    connection.Open();    
    using (OdbcDataAdapter adapter = new OdbcDataAdapter(command))
    {
        adapter.Fill(dataTable);
    }
}

Upvotes: 7

Related Questions