SQL Parameterized Store Procedure by using the Odbc

  • Ara
    05
  • 0


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

            command.Parameters.Add("@model", OdbcType.NVarChar, 16).Value = model;
            command.Parameters.Add("@type", OdbcType.NVarChar, 6).Value = type;
            command.Parameters.Add("@country", OdbcType.NVarChar, 10).Value = country;
            command.Parameters.Add("@market", OdbcType.NVarChar, 6).Value = market;

            if (connection.State == ConnectionState.Open)
            {
                var reader = command.ExecuteReader();
                while (reader.Read())
                {
                    retVal.Add(reader.GetString(reader.GetOrdinal("allowed"));
                }
            }
            connection.Close();
        }

        Another type of using;

        OdbcConnection connection = new OdbcConnection(connectionString);

         var command = new OdbcCommand
         {
               CommandText = "{CALL mystoreprocedureforlogon (?,?)}",
               CommandType = CommandType.StoredProcedure,
               Connection = connection
         };

         var parameter = command.Parameters.Add("@userId ", OdbcType.VarChar, 30);
         parameter.Value = UserName;
         parameter = command.Parameters.Add("@password", OdbcType.VarChar, 15);
         parameter.Value = Password;

          if (connection.State == ConnectionState.Open)
         {
                OdbcDataReader Reader = command.ExecuteReader();
                if (Reader.HasRows)
                {
                       //do something with your data
                }
         }
         connection.Close();

Hiç yorum yok:

Yorum Gönder